Tracepoint API namespacing 'TRACEPOINT_PROBE_DYNAMIC_LINKAGE'
[lttng-ust.git] / include / lttng / tracepoint.h
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 */
6
7 #ifndef _LTTNG_TRACEPOINT_H
8 #define _LTTNG_TRACEPOINT_H
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <lttng/tracepoint-types.h>
13 #include <lttng/tracepoint-rcu.h>
14 #include <urcu/compiler.h>
15 #include <urcu/system.h>
16 #include <dlfcn.h> /* for dlopen */
17 #include <string.h> /* for memset */
18
19 #include <lttng/ust-config.h> /* for sdt */
20 #include <lttng/ust-compiler.h>
21 #include <lttng/ust-tracer.h>
22 #include <lttng/ust-api-compat.h>
23
24 #define LTTNG_UST_TRACEPOINT_NAME_LEN_MAX 256
25
26 #ifdef LTTNG_UST_HAVE_SDT_INTEGRATION
27 /*
28 * Instead of using SDT_USE_VARIADIC from 'sys/sdt.h', use our own namespaced
29 * macros since the instrumented application might already have included
30 * 'sys/sdt.h' without variadic support.
31 */
32 #include <sys/sdt.h>
33
34 #define LTTNG_UST__SDT_NARG(...) \
35 LTTNG_UST___SDT_NARG(__VA_ARGS__, 12,11,10,9,8,7,6,5,4,3,2,1,0)
36
37 #define LTTNG_UST___SDT_NARG(_0,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12, N, ...) N
38
39 #define LTTNG_UST__SDT_PROBE_N(provider, name, N, ...) \
40 _SDT_PROBE(provider, name, N, (__VA_ARGS__))
41
42 #define LTTNG_UST_STAP_PROBEV(provider, name, ...) \
43 LTTNG_UST__SDT_PROBE_N(provider, name, LTTNG_UST__SDT_NARG(0, ##__VA_ARGS__), ##__VA_ARGS__)
44
45 #else
46 #define LTTNG_UST_STAP_PROBEV(...)
47 #endif
48
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52
53 #define lttng_ust_tracepoint_enabled(provider, name) \
54 caa_unlikely(CMM_LOAD_SHARED(lttng_ust_tracepoint_##provider##___##name.state))
55
56 #define lttng_ust_do_tracepoint(provider, name, ...) \
57 lttng_ust_tracepoint_cb_##provider##___##name(__VA_ARGS__)
58
59 #define lttng_ust_tracepoint(provider, name, ...) \
60 do { \
61 LTTNG_UST_STAP_PROBEV(provider, name, ## __VA_ARGS__); \
62 if (lttng_ust_tracepoint_enabled(provider, name)) \
63 lttng_ust_do_tracepoint(provider, name, __VA_ARGS__); \
64 } while (0)
65
66 #define LTTNG_UST_TP_ARGS(...) __VA_ARGS__
67
68 /*
69 * LTTNG_UST_TP_ARGS takes tuples of type, argument separated by a comma.
70 * It can take up to 10 tuples (which means that less than 10 tuples is
71 * fine too).
72 * Each tuple is also separated by a comma.
73 */
74 #define LTTNG_UST___TP_COMBINE_TOKENS(_tokena, _tokenb) \
75 _tokena##_tokenb
76 #define LTTNG_UST__TP_COMBINE_TOKENS(_tokena, _tokenb) \
77 LTTNG_UST___TP_COMBINE_TOKENS(_tokena, _tokenb)
78 #define LTTNG_UST___TP_COMBINE_TOKENS3(_tokena, _tokenb, _tokenc) \
79 _tokena##_tokenb##_tokenc
80 #define LTTNG_UST__TP_COMBINE_TOKENS3(_tokena, _tokenb, _tokenc) \
81 LTTNG_UST___TP_COMBINE_TOKENS3(_tokena, _tokenb, _tokenc)
82 #define LTTNG_UST___TP_COMBINE_TOKENS4(_tokena, _tokenb, _tokenc, _tokend) \
83 _tokena##_tokenb##_tokenc##_tokend
84 #define LTTNG_UST__TP_COMBINE_TOKENS4(_tokena, _tokenb, _tokenc, _tokend) \
85 LTTNG_UST___TP_COMBINE_TOKENS4(_tokena, _tokenb, _tokenc, _tokend)
86
87 /*
88 * LTTNG_UST__TP_EXVAR* extract the var names.
89 * LTTNG_UST__TP_EXVAR1 and LTTNG_UST__TP_EXDATA_VAR1 are needed for -std=c99.
90 */
91 #define LTTNG_UST__TP_EXVAR0()
92 #define LTTNG_UST__TP_EXVAR1(a)
93 #define LTTNG_UST__TP_EXVAR2(a,b) b
94 #define LTTNG_UST__TP_EXVAR4(a,b,c,d) b,d
95 #define LTTNG_UST__TP_EXVAR6(a,b,c,d,e,f) b,d,f
96 #define LTTNG_UST__TP_EXVAR8(a,b,c,d,e,f,g,h) b,d,f,h
97 #define LTTNG_UST__TP_EXVAR10(a,b,c,d,e,f,g,h,i,j) b,d,f,h,j
98 #define LTTNG_UST__TP_EXVAR12(a,b,c,d,e,f,g,h,i,j,k,l) b,d,f,h,j,l
99 #define LTTNG_UST__TP_EXVAR14(a,b,c,d,e,f,g,h,i,j,k,l,m,n) b,d,f,h,j,l,n
100 #define LTTNG_UST__TP_EXVAR16(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) b,d,f,h,j,l,n,p
101 #define LTTNG_UST__TP_EXVAR18(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r) b,d,f,h,j,l,n,p,r
102 #define LTTNG_UST__TP_EXVAR20(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t) b,d,f,h,j,l,n,p,r,t
103
104 #define LTTNG_UST__TP_EXDATA_VAR0() __tp_data
105 #define LTTNG_UST__TP_EXDATA_VAR1(a) __tp_data
106 #define LTTNG_UST__TP_EXDATA_VAR2(a,b) __tp_data,b
107 #define LTTNG_UST__TP_EXDATA_VAR4(a,b,c,d) __tp_data,b,d
108 #define LTTNG_UST__TP_EXDATA_VAR6(a,b,c,d,e,f) __tp_data,b,d,f
109 #define LTTNG_UST__TP_EXDATA_VAR8(a,b,c,d,e,f,g,h) __tp_data,b,d,f,h
110 #define LTTNG_UST__TP_EXDATA_VAR10(a,b,c,d,e,f,g,h,i,j) __tp_data,b,d,f,h,j
111 #define LTTNG_UST__TP_EXDATA_VAR12(a,b,c,d,e,f,g,h,i,j,k,l) __tp_data,b,d,f,h,j,l
112 #define LTTNG_UST__TP_EXDATA_VAR14(a,b,c,d,e,f,g,h,i,j,k,l,m,n) __tp_data,b,d,f,h,j,l,n
113 #define LTTNG_UST__TP_EXDATA_VAR16(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) __tp_data,b,d,f,h,j,l,n,p
114 #define LTTNG_UST__TP_EXDATA_VAR18(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r) __tp_data,b,d,f,h,j,l,n,p,r
115 #define LTTNG_UST__TP_EXDATA_VAR20(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t) __tp_data,b,d,f,h,j,l,n,p,r,t
116
117 /*
118 * LTTNG_UST__TP_EXPROTO* extract tuples of type, var.
119 * LTTNG_UST__TP_EXPROTO1 and LTTNG_UST__TP_EXDATA_PROTO1 are needed for -std=c99.
120 */
121 #define LTTNG_UST__TP_EXPROTO0() void
122 #define LTTNG_UST__TP_EXPROTO1(a) void
123 #define LTTNG_UST__TP_EXPROTO2(a,b) a b
124 #define LTTNG_UST__TP_EXPROTO4(a,b,c,d) a b,c d
125 #define LTTNG_UST__TP_EXPROTO6(a,b,c,d,e,f) a b,c d,e f
126 #define LTTNG_UST__TP_EXPROTO8(a,b,c,d,e,f,g,h) a b,c d,e f,g h
127 #define LTTNG_UST__TP_EXPROTO10(a,b,c,d,e,f,g,h,i,j) a b,c d,e f,g h,i j
128 #define LTTNG_UST__TP_EXPROTO12(a,b,c,d,e,f,g,h,i,j,k,l) a b,c d,e f,g h,i j,k l
129 #define LTTNG_UST__TP_EXPROTO14(a,b,c,d,e,f,g,h,i,j,k,l,m,n) a b,c d,e f,g h,i j,k l,m n
130 #define LTTNG_UST__TP_EXPROTO16(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) a b,c d,e f,g h,i j,k l,m n,o p
131 #define LTTNG_UST__TP_EXPROTO18(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r) a b,c d,e f,g h,i j,k l,m n,o p,q r
132 #define LTTNG_UST__TP_EXPROTO20(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t) a b,c d,e f,g h,i j,k l,m n,o p,q r,s t
133
134 #define LTTNG_UST__TP_EXDATA_PROTO0() void *__tp_data
135 #define LTTNG_UST__TP_EXDATA_PROTO1(a) void *__tp_data
136 #define LTTNG_UST__TP_EXDATA_PROTO2(a,b) void *__tp_data,a b
137 #define LTTNG_UST__TP_EXDATA_PROTO4(a,b,c,d) void *__tp_data,a b,c d
138 #define LTTNG_UST__TP_EXDATA_PROTO6(a,b,c,d,e,f) void *__tp_data,a b,c d,e f
139 #define LTTNG_UST__TP_EXDATA_PROTO8(a,b,c,d,e,f,g,h) void *__tp_data,a b,c d,e f,g h
140 #define LTTNG_UST__TP_EXDATA_PROTO10(a,b,c,d,e,f,g,h,i,j) void *__tp_data,a b,c d,e f,g h,i j
141 #define LTTNG_UST__TP_EXDATA_PROTO12(a,b,c,d,e,f,g,h,i,j,k,l) void *__tp_data,a b,c d,e f,g h,i j,k l
142 #define LTTNG_UST__TP_EXDATA_PROTO14(a,b,c,d,e,f,g,h,i,j,k,l,m,n) void *__tp_data,a b,c d,e f,g h,i j,k l,m n
143 #define LTTNG_UST__TP_EXDATA_PROTO16(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) void *__tp_data,a b,c d,e f,g h,i j,k l,m n,o p
144 #define LTTNG_UST__TP_EXDATA_PROTO18(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r) void *__tp_data,a b,c d,e f,g h,i j,k l,m n,o p,q r
145 #define LTTNG_UST__TP_EXDATA_PROTO20(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t) void *__tp_data,a b,c d,e f,g h,i j,k l,m n,o p,q r,s t
146
147 /* Preprocessor trick to count arguments. Inspired from sdt.h. */
148 #define LTTNG_UST__TP_NARGS(...) LTTNG_UST___TP_NARGS(__VA_ARGS__, 20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0)
149 #define LTTNG_UST___TP_NARGS(_0,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20, N, ...) N
150 #define LTTNG_UST__TP_PROTO_N(N, ...) LTTNG_UST__TP_PARAMS(LTTNG_UST__TP_COMBINE_TOKENS(LTTNG_UST__TP_EXPROTO, N)(__VA_ARGS__))
151 #define LTTNG_UST__TP_VAR_N(N, ...) LTTNG_UST__TP_PARAMS(LTTNG_UST__TP_COMBINE_TOKENS(LTTNG_UST__TP_EXVAR, N)(__VA_ARGS__))
152 #define LTTNG_UST__TP_DATA_PROTO_N(N, ...) LTTNG_UST__TP_PARAMS(LTTNG_UST__TP_COMBINE_TOKENS(LTTNG_UST__TP_EXDATA_PROTO, N)(__VA_ARGS__))
153 #define LTTNG_UST__TP_DATA_VAR_N(N, ...) LTTNG_UST__TP_PARAMS(LTTNG_UST__TP_COMBINE_TOKENS(LTTNG_UST__TP_EXDATA_VAR, N)(__VA_ARGS__))
154 #define LTTNG_UST__TP_ARGS_PROTO(...) LTTNG_UST__TP_PROTO_N(LTTNG_UST__TP_NARGS(0, ##__VA_ARGS__), ##__VA_ARGS__)
155 #define LTTNG_UST__TP_ARGS_VAR(...) LTTNG_UST__TP_VAR_N(LTTNG_UST__TP_NARGS(0, ##__VA_ARGS__), ##__VA_ARGS__)
156 #define LTTNG_UST__TP_ARGS_DATA_PROTO(...) LTTNG_UST__TP_DATA_PROTO_N(LTTNG_UST__TP_NARGS(0, ##__VA_ARGS__), ##__VA_ARGS__)
157 #define LTTNG_UST__TP_ARGS_DATA_VAR(...) LTTNG_UST__TP_DATA_VAR_N(LTTNG_UST__TP_NARGS(0, ##__VA_ARGS__), ##__VA_ARGS__)
158 #define LTTNG_UST__TP_PARAMS(...) __VA_ARGS__
159
160 /*
161 * sizeof(#_provider) - 1 : length of the provider string (excluding \0).
162 * sizeof(#_name) - 1 : length of the name string (excluding \0).
163 * + 1 : separator between provider and event name.
164 *
165 * Upper bound (inclusive) is LTTNG_UST_TRACEPOINT_NAME_LEN_MAX - 1 to
166 * account for \0.
167 *
168 * The comparison is:
169 * left hand side: sizeof(#_provider) - 1 + sizeof(#_name) - 1 + 1
170 * right hand side: LTTNG_UST_TRACEPOINT_NAME_LEN_MAX - 1
171 * operator: <= (inclusive)
172 * Simplified in the code below.
173 */
174 #define lttng_ust_tracepoint_validate_name_len(_provider, _name) \
175 lttng_ust_static_assert(sizeof(#_provider) + sizeof(#_name) <= LTTNG_UST_TRACEPOINT_NAME_LEN_MAX, \
176 "Tracepoint name length is too long", \
177 Tracepoint_name_length_is_too_long)
178
179 /*
180 * The tracepoint cb is marked always inline so we can distinguish
181 * between caller's ip addresses within the probe using the return
182 * address.
183 */
184 #define LTTNG_UST__DECLARE_TRACEPOINT(_provider, _name, ...) \
185 extern struct lttng_ust_tracepoint lttng_ust_tracepoint_##_provider##___##_name; \
186 static inline \
187 void lttng_ust_tracepoint_cb_##_provider##___##_name(LTTNG_UST__TP_ARGS_PROTO(__VA_ARGS__)) \
188 __attribute__((always_inline, unused)) lttng_ust_notrace; \
189 static \
190 void lttng_ust_tracepoint_cb_##_provider##___##_name(LTTNG_UST__TP_ARGS_PROTO(__VA_ARGS__)) \
191 { \
192 struct lttng_ust_tracepoint_probe *__tp_probe; \
193 \
194 if (caa_unlikely(!TP_RCU_LINK_TEST())) \
195 return; \
196 tp_rcu_read_lock(); \
197 __tp_probe = tp_rcu_dereference(lttng_ust_tracepoint_##_provider##___##_name.probes); \
198 if (caa_unlikely(!__tp_probe)) \
199 goto end; \
200 do { \
201 void (*__tp_cb)(void) = __tp_probe->func; \
202 void *__tp_data = __tp_probe->data; \
203 \
204 URCU_FORCE_CAST(void (*)(LTTNG_UST__TP_ARGS_DATA_PROTO(__VA_ARGS__)), __tp_cb) \
205 (LTTNG_UST__TP_ARGS_DATA_VAR(__VA_ARGS__)); \
206 } while ((++__tp_probe)->func); \
207 end: \
208 tp_rcu_read_unlock(); \
209 } \
210 static inline \
211 void lttng_ust_tracepoint_register_##_provider##___##_name(char *provider_name, char *event_name, \
212 void (*func)(void), void *data) \
213 lttng_ust_notrace; \
214 static inline \
215 void lttng_ust_tracepoint_register_##_provider##___##_name(char *provider_name, char *event_name, \
216 void (*func)(void), void *data) \
217 { \
218 lttng_ust_tracepoint_probe_register(provider_name, event_name, func, data, \
219 lttng_ust_tracepoint_##_provider##___##_name.signature); \
220 } \
221 static inline \
222 void lttng_ust_tracepoint_unregister_##_provider##___##_name(char *provider_name, char *event_name, \
223 void (*func)(void), void *data) \
224 lttng_ust_notrace; \
225 static inline \
226 void lttng_ust_tracepoint_unregister_##_provider##___##_name(char *provider_name, char *event_name, \
227 void (*func)(void), void *data) \
228 { \
229 lttng_ust_tracepoint_probe_unregister(provider_name, event_name, func, data); \
230 }
231
232 extern int lttng_ust_tracepoint_probe_register(const char *provider_name, const char *event_name,
233 void (*func)(void), void *data, const char *signature);
234 extern int lttng_ust_tracepoint_probe_unregister(const char *provider_name, const char *event_name,
235 void (*func)(void), void *data);
236
237 /*
238 * tracepoint dynamic linkage handling (callbacks). Hidden visibility:
239 * shared across objects in a module/main executable.
240 *
241 * IMPORTANT: this structure is part of the ABI between instrumented
242 * applications and UST. Fields need to be only added at the end, never
243 * reordered, never removed.
244 *
245 * The field @struct_size should be used to determine the size of the
246 * structure. It should be queried before using additional fields added
247 * at the end of the structure.
248 */
249 struct lttng_ust_tracepoint_dlopen {
250 uint32_t struct_size;
251
252 void *liblttngust_handle;
253
254 int (*tracepoint_register_lib)(struct lttng_ust_tracepoint * const *tracepoints_start,
255 int tracepoints_count);
256 int (*tracepoint_unregister_lib)(struct lttng_ust_tracepoint * const *tracepoints_start);
257 void (*rcu_read_lock_sym)(void);
258 void (*rcu_read_unlock_sym)(void);
259 void *(*rcu_dereference_sym)(void *p);
260
261 /* End of base ABI. Fields below should be used after checking struct_size. */
262 };
263
264 extern struct lttng_ust_tracepoint_dlopen lttng_ust_tracepoint_dlopen;
265 extern struct lttng_ust_tracepoint_dlopen *lttng_ust_tracepoint_dlopen_ptr;
266
267 /*
268 * These weak symbols, the constructor, and destructor take care of
269 * registering only _one_ instance of the tracepoints per shared-ojbect
270 * (or for the whole main program).
271 */
272 int lttng_ust_tracepoint_registered
273 __attribute__((weak, visibility("hidden")));
274 int lttng_ust_tracepoint_ptrs_registered
275 __attribute__((weak, visibility("hidden")));
276 struct lttng_ust_tracepoint_dlopen lttng_ust_tracepoint_dlopen
277 __attribute__((weak, visibility("hidden"))) = {
278 .struct_size = sizeof(struct lttng_ust_tracepoint_dlopen),
279 };
280 /*
281 * Deal with gcc O1 optimisation issues with weak hidden symbols. gcc
282 * 4.8 and prior does not have the same behavior for symbol scoping on
283 * 32-bit powerpc depending on the object size: symbols for objects of 8
284 * bytes or less have the same address throughout a module, whereas they
285 * have different addresses between compile units for objects larger
286 * than 8 bytes. Add this pointer indirection to ensure that the symbol
287 * scoping match that of the other weak hidden symbols found in this
288 * header.
289 */
290 struct lttng_ust_tracepoint_dlopen *lttng_ust_tracepoint_dlopen_ptr
291 __attribute__((weak, visibility("hidden")));
292
293 /*
294 * Tracepoint dynamic linkage handling (callbacks). Hidden visibility: shared
295 * across objects in a module/main executable. The callbacks are used to
296 * control and check if the destructors should be executed.
297 *
298 * IMPORTANT: this structure is part of the ABI between instrumented
299 * applications and UST. Fields need to be only added at the end, never
300 * reordered, never removed.
301 *
302 * The field @struct_size should be used to determine the size of the
303 * structure. It should be queried before using additional fields added
304 * at the end of the structure.
305 */
306 struct lttng_ust_tracepoint_destructors_syms {
307 uint32_t struct_size;
308
309 void (*tracepoint_disable_destructors)(void);
310 int (*tracepoint_get_destructors_state)(void);
311
312 /* End of base ABI. Fields below should be used after checking struct_size. */
313 };
314
315 extern struct lttng_ust_tracepoint_destructors_syms lttng_ust_tracepoint_destructors_syms;
316 extern struct lttng_ust_tracepoint_destructors_syms *lttng_ust_tracepoint_destructors_syms_ptr;
317
318 struct lttng_ust_tracepoint_destructors_syms lttng_ust_tracepoint_destructors_syms
319 __attribute__((weak, visibility("hidden"))) = {
320 .struct_size = sizeof(struct lttng_ust_tracepoint_destructors_syms),
321 };
322 struct lttng_ust_tracepoint_destructors_syms *lttng_ust_tracepoint_destructors_syms_ptr
323 __attribute__((weak, visibility("hidden")));
324
325 static inline void tracepoint_disable_destructors(void)
326 {
327 if (!lttng_ust_tracepoint_dlopen_ptr)
328 lttng_ust_tracepoint_dlopen_ptr = &lttng_ust_tracepoint_dlopen;
329 if (!lttng_ust_tracepoint_destructors_syms_ptr)
330 lttng_ust_tracepoint_destructors_syms_ptr = &lttng_ust_tracepoint_destructors_syms;
331 if (lttng_ust_tracepoint_dlopen_ptr->liblttngust_handle
332 && lttng_ust_tracepoint_destructors_syms_ptr->tracepoint_disable_destructors)
333 lttng_ust_tracepoint_destructors_syms_ptr->tracepoint_disable_destructors();
334 }
335
336 #ifndef _LGPL_SOURCE
337 static inline void
338 lttng_ust_tracepoint__init_urcu_sym(void)
339 lttng_ust_notrace;
340 static inline void
341 lttng_ust_tracepoint__init_urcu_sym(void)
342 {
343 if (!lttng_ust_tracepoint_dlopen_ptr)
344 lttng_ust_tracepoint_dlopen_ptr = &lttng_ust_tracepoint_dlopen;
345 /*
346 * Symbols below are needed by tracepoint call sites and probe
347 * providers.
348 */
349 if (!lttng_ust_tracepoint_dlopen_ptr->rcu_read_lock_sym)
350 lttng_ust_tracepoint_dlopen_ptr->rcu_read_lock_sym =
351 URCU_FORCE_CAST(void (*)(void),
352 dlsym(lttng_ust_tracepoint_dlopen_ptr->liblttngust_handle,
353 "tp_rcu_read_lock"));
354 if (!lttng_ust_tracepoint_dlopen_ptr->rcu_read_unlock_sym)
355 lttng_ust_tracepoint_dlopen_ptr->rcu_read_unlock_sym =
356 URCU_FORCE_CAST(void (*)(void),
357 dlsym(lttng_ust_tracepoint_dlopen_ptr->liblttngust_handle,
358 "tp_rcu_read_unlock"));
359 if (!lttng_ust_tracepoint_dlopen_ptr->rcu_dereference_sym)
360 lttng_ust_tracepoint_dlopen_ptr->rcu_dereference_sym =
361 URCU_FORCE_CAST(void *(*)(void *p),
362 dlsym(lttng_ust_tracepoint_dlopen_ptr->liblttngust_handle,
363 "tp_rcu_dereference_sym"));
364 }
365 #else
366 static inline void
367 lttng_ust_tracepoint__init_urcu_sym(void)
368 lttng_ust_notrace;
369 static inline void
370 lttng_ust_tracepoint__init_urcu_sym(void)
371 {
372 }
373 #endif
374
375 static void
376 lttng_ust__tracepoints__init(void)
377 lttng_ust_notrace __attribute__((constructor));
378 static void
379 lttng_ust__tracepoints__init(void)
380 {
381 if (lttng_ust_tracepoint_registered++) {
382 if (!lttng_ust_tracepoint_dlopen_ptr->liblttngust_handle)
383 return;
384 lttng_ust_tracepoint__init_urcu_sym();
385 return;
386 }
387
388 if (!lttng_ust_tracepoint_dlopen_ptr)
389 lttng_ust_tracepoint_dlopen_ptr = &lttng_ust_tracepoint_dlopen;
390 if (!lttng_ust_tracepoint_dlopen_ptr->liblttngust_handle)
391 lttng_ust_tracepoint_dlopen_ptr->liblttngust_handle =
392 dlopen(LTTNG_UST_TRACEPOINT_LIB_SONAME, RTLD_NOW | RTLD_GLOBAL);
393 if (!lttng_ust_tracepoint_dlopen_ptr->liblttngust_handle)
394 return;
395 lttng_ust_tracepoint__init_urcu_sym();
396 }
397
398 static void
399 lttng_ust__tracepoints__destroy(void)
400 lttng_ust_notrace __attribute__((destructor));
401 static void
402 lttng_ust__tracepoints__destroy(void)
403 {
404 int ret;
405
406 if (--lttng_ust_tracepoint_registered)
407 return;
408 if (!lttng_ust_tracepoint_dlopen_ptr)
409 lttng_ust_tracepoint_dlopen_ptr = &lttng_ust_tracepoint_dlopen;
410 if (!lttng_ust_tracepoint_destructors_syms_ptr)
411 lttng_ust_tracepoint_destructors_syms_ptr = &lttng_ust_tracepoint_destructors_syms;
412 if (!lttng_ust_tracepoint_dlopen_ptr->liblttngust_handle)
413 return;
414 if (lttng_ust_tracepoint_ptrs_registered)
415 return;
416 /*
417 * Lookup if destructors must be executed using the new method.
418 */
419 if (lttng_ust_tracepoint_destructors_syms_ptr->tracepoint_get_destructors_state
420 && !lttng_ust_tracepoint_destructors_syms_ptr->tracepoint_get_destructors_state()) {
421 /*
422 * The tracepoint_get_destructors_state symbol was found with
423 * dlsym but its returned value is 0 meaning that destructors
424 * must not be executed.
425 */
426 return;
427 }
428 ret = dlclose(lttng_ust_tracepoint_dlopen_ptr->liblttngust_handle);
429 if (ret) {
430 fprintf(stderr, "Error (%d) in dlclose\n", ret);
431 abort();
432 }
433 memset(lttng_ust_tracepoint_dlopen_ptr, 0, sizeof(*lttng_ust_tracepoint_dlopen_ptr));
434 }
435
436 #ifdef TRACEPOINT_DEFINE
437
438 /*
439 * These weak symbols, the constructor, and destructor take care of
440 * registering only _one_ instance of the tracepoints per shared-ojbect
441 * (or for the whole main program).
442 */
443 extern struct lttng_ust_tracepoint * const __start_lttng_ust_tracepoints_ptrs[]
444 __attribute__((weak, visibility("hidden")));
445 extern struct lttng_ust_tracepoint * const __stop_lttng_ust_tracepoints_ptrs[]
446 __attribute__((weak, visibility("hidden")));
447
448 /*
449 * When LTTNG_UST_TRACEPOINT_PROBE_DYNAMIC_LINKAGE is defined, we do not emit a
450 * unresolved symbol that requires the provider to be linked in. When
451 * LTTNG_UST_TRACEPOINT_PROBE_DYNAMIC_LINKAGE is not defined, we emit an
452 * unresolved symbol that depends on having the provider linked in,
453 * otherwise the linker complains. This deals with use of static
454 * libraries, ensuring that the linker does not remove the provider
455 * object from the executable.
456 */
457
458 #if LTTNG_UST_COMPAT_API(0)
459 # if defined(TRACEPOINT_PROBE_DYNAMIC_LINKAGE) && !defined(LTTNG_UST_TRACEPOINT_PROBE_DYNAMIC_LINKAGE)
460 # define LTTNG_UST_TRACEPOINT_PROBE_DYNAMIC_LINKAGE
461 # endif
462 #endif /* #if LTTNG_UST_COMPAT_API(0) */
463
464 #ifdef LTTNG_UST_TRACEPOINT_PROBE_DYNAMIC_LINKAGE
465 #define LTTNG_UST__TRACEPOINT_UNDEFINED_REF(provider) NULL
466 #else /* LTTNG_UST_TRACEPOINT_PROBE_DYNAMIC_LINKAGE */
467 #define LTTNG_UST__TRACEPOINT_UNDEFINED_REF(provider) &lttng_ust_tracepoint_provider_##provider
468 #endif /* LTTNG_UST_TRACEPOINT_PROBE_DYNAMIC_LINKAGE */
469
470 /*
471 * Note: to allow PIC code, we need to allow the linker to update the pointers
472 * in the lttng_ust_tracepoints_ptrs section.
473 * Therefore, this section is _not_ const (read-only).
474 */
475 #define LTTNG_UST__TP_EXTRACT_STRING(...) #__VA_ARGS__
476
477 #define LTTNG_UST__DEFINE_TRACEPOINT(_provider, _name, _args) \
478 lttng_ust_tracepoint_validate_name_len(_provider, _name); \
479 extern int lttng_ust_tracepoint_provider_##_provider; \
480 static const char __tp_provider_strtab_##_provider##___##_name[] \
481 __attribute__((section("lttng_ust_tracepoints_strings"))) = \
482 #_provider; \
483 static const char __tp_name_strtab_##_provider##___##_name[] \
484 __attribute__((section("lttng_ust_tracepoints_strings"))) = \
485 #_name; \
486 struct lttng_ust_tracepoint lttng_ust_tracepoint_##_provider##___##_name \
487 __attribute__((section("lttng_ust_tracepoints"))) = { \
488 sizeof(struct lttng_ust_tracepoint), \
489 __tp_provider_strtab_##_provider##___##_name, \
490 __tp_name_strtab_##_provider##___##_name, \
491 0, \
492 NULL, \
493 LTTNG_UST__TRACEPOINT_UNDEFINED_REF(_provider), \
494 LTTNG_UST__TP_EXTRACT_STRING(_args), \
495 }; \
496 static struct lttng_ust_tracepoint * \
497 lttng_ust_tracepoint_ptr_##_provider##___##_name \
498 __attribute__((section("lttng_ust_tracepoints_ptrs"), used)) \
499 __lttng_ust_variable_attribute_no_sanitize_address = \
500 &lttng_ust_tracepoint_##_provider##___##_name;
501
502 static void
503 lttng_ust__tracepoints__ptrs_init(void)
504 lttng_ust_notrace __attribute__((constructor));
505 static void
506 lttng_ust__tracepoints__ptrs_init(void)
507 {
508 if (lttng_ust_tracepoint_ptrs_registered++)
509 return;
510 if (!lttng_ust_tracepoint_dlopen_ptr)
511 lttng_ust_tracepoint_dlopen_ptr = &lttng_ust_tracepoint_dlopen;
512 if (!lttng_ust_tracepoint_dlopen_ptr->liblttngust_handle)
513 lttng_ust_tracepoint_dlopen_ptr->liblttngust_handle =
514 dlopen(LTTNG_UST_TRACEPOINT_LIB_SONAME, RTLD_NOW | RTLD_GLOBAL);
515 if (!lttng_ust_tracepoint_dlopen_ptr->liblttngust_handle)
516 return;
517 if (!lttng_ust_tracepoint_destructors_syms_ptr)
518 lttng_ust_tracepoint_destructors_syms_ptr = &lttng_ust_tracepoint_destructors_syms;
519 lttng_ust_tracepoint_dlopen_ptr->tracepoint_register_lib =
520 URCU_FORCE_CAST(int (*)(struct lttng_ust_tracepoint * const *, int),
521 dlsym(lttng_ust_tracepoint_dlopen_ptr->liblttngust_handle,
522 "tracepoint_register_lib"));
523 lttng_ust_tracepoint_dlopen_ptr->tracepoint_unregister_lib =
524 URCU_FORCE_CAST(int (*)(struct lttng_ust_tracepoint * const *),
525 dlsym(lttng_ust_tracepoint_dlopen_ptr->liblttngust_handle,
526 "tracepoint_unregister_lib"));
527 lttng_ust_tracepoint_destructors_syms_ptr->tracepoint_disable_destructors =
528 URCU_FORCE_CAST(void (*)(void),
529 dlsym(lttng_ust_tracepoint_dlopen_ptr->liblttngust_handle,
530 "tp_disable_destructors"));
531 lttng_ust_tracepoint_destructors_syms_ptr->tracepoint_get_destructors_state =
532 URCU_FORCE_CAST(int (*)(void),
533 dlsym(lttng_ust_tracepoint_dlopen_ptr->liblttngust_handle,
534 "tp_get_destructors_state"));
535 lttng_ust_tracepoint__init_urcu_sym();
536 if (lttng_ust_tracepoint_dlopen_ptr->tracepoint_register_lib) {
537 lttng_ust_tracepoint_dlopen_ptr->tracepoint_register_lib(__start_lttng_ust_tracepoints_ptrs,
538 __stop_lttng_ust_tracepoints_ptrs -
539 __start_lttng_ust_tracepoints_ptrs);
540 }
541 }
542
543 static void
544 lttng_ust__tracepoints__ptrs_destroy(void)
545 lttng_ust_notrace __attribute__((destructor));
546 static void
547 lttng_ust__tracepoints__ptrs_destroy(void)
548 {
549 int ret;
550
551 if (--lttng_ust_tracepoint_ptrs_registered)
552 return;
553 if (!lttng_ust_tracepoint_dlopen_ptr)
554 lttng_ust_tracepoint_dlopen_ptr = &lttng_ust_tracepoint_dlopen;
555 if (!lttng_ust_tracepoint_destructors_syms_ptr)
556 lttng_ust_tracepoint_destructors_syms_ptr = &lttng_ust_tracepoint_destructors_syms;
557 if (lttng_ust_tracepoint_dlopen_ptr->tracepoint_unregister_lib)
558 lttng_ust_tracepoint_dlopen_ptr->tracepoint_unregister_lib(__start_lttng_ust_tracepoints_ptrs);
559 if (lttng_ust_tracepoint_dlopen_ptr->liblttngust_handle
560 && lttng_ust_tracepoint_destructors_syms_ptr->tracepoint_get_destructors_state
561 && lttng_ust_tracepoint_destructors_syms_ptr->tracepoint_get_destructors_state()
562 && !lttng_ust_tracepoint_ptrs_registered) {
563 ret = dlclose(lttng_ust_tracepoint_dlopen_ptr->liblttngust_handle);
564 if (ret) {
565 fprintf(stderr, "Error (%d) in dlclose\n", ret);
566 abort();
567 }
568 memset(lttng_ust_tracepoint_dlopen_ptr, 0, sizeof(*lttng_ust_tracepoint_dlopen_ptr));
569 }
570 }
571
572 #else /* TRACEPOINT_DEFINE */
573
574 #define LTTNG_UST__DEFINE_TRACEPOINT(_provider, _name, _args)
575
576 #endif /* #else TRACEPOINT_DEFINE */
577
578 #if LTTNG_UST_COMPAT_API(0)
579 #define tracepoint lttng_ust_tracepoint
580 #define do_tracepoint lttng_ust_do_tracepoint
581 #define tracepoint_enabled lttng_ust_tracepoint_enabled
582 #define TP_ARGS LTTNG_UST_TP_ARGS
583 #define TP_FIELDS LTTNG_UST_TP_FIELDS
584 #endif /* #if LTTNG_UST_COMPAT_API(0) */
585
586 #ifdef __cplusplus
587 }
588 #endif
589
590 #endif /* _LTTNG_TRACEPOINT_H */
591
592 /* The following declarations must be outside re-inclusion protection. */
593
594 #ifndef TRACEPOINT_ENUM
595
596 /*
597 * Tracepoint Enumerations
598 *
599 * The enumeration is a mapping between an integer, or range of integers, and
600 * a string. It can be used to have a more compact trace in cases where the
601 * possible values for a field are limited:
602 *
603 * An example:
604 *
605 * TRACEPOINT_ENUM(someproject_component, enumname,
606 * TP_ENUM_VALUES(
607 * ctf_enum_value("even", 0)
608 * ctf_enum_value("uneven", 1)
609 * ctf_enum_range("twoto4", 2, 4)
610 * ctf_enum_value("five", 5)
611 * )
612 * )
613 *
614 * Where "someproject_component" is the name of the component this enumeration
615 * belongs to and "enumname" identifies this enumeration. Inside the
616 * TP_ENUM_VALUES macro is the actual mapping. Each string value can map
617 * to either a single value with ctf_enum_value or a range of values
618 * with ctf_enum_range.
619 *
620 * Enumeration ranges may overlap, but the behavior is implementation-defined,
621 * each trace reader will handle overlapping as it wishes.
622 *
623 * That enumeration can then be used in a field inside the TP_FIELD macro using
624 * the following line:
625 *
626 * ctf_enum(someproject_component, enumname, enumtype, enumfield, enumval)
627 *
628 * Where "someproject_component" and "enumname" match those in the
629 * TRACEPOINT_ENUM, "enumtype" is a signed or unsigned integer type
630 * backing the enumeration, "enumfield" is the name of the field and
631 * "enumval" is the value.
632 */
633
634 #define TRACEPOINT_ENUM(provider, name, values)
635
636 #endif /* #ifndef TRACEPOINT_ENUM */
637
638 #ifndef LTTNG_UST_TRACEPOINT_EVENT
639
640 /*
641 * How to use the LTTNG_UST_TRACEPOINT_EVENT macro:
642 *
643 * An example:
644 *
645 * LTTNG_UST_TRACEPOINT_EVENT(someproject_component, event_name,
646 *
647 * * LTTNG_UST_TP_ARGS takes from 0 to 10 "type, field_name" pairs *
648 *
649 * LTTNG_UST_TP_ARGS(int, arg0, void *, arg1, char *, string, size_t, strlen,
650 * long *, arg4, size_t, arg4_len),
651 *
652 * * LTTNG_UST_TP_FIELDS describes the event payload layout in the trace *
653 *
654 * LTTNG_UST_TP_FIELDS(
655 * * Integer, printed in base 10 *
656 * ctf_integer(int, field_a, arg0)
657 *
658 * * Integer, printed with 0x base 16 *
659 * ctf_integer_hex(unsigned long, field_d, arg1)
660 *
661 * * Enumeration *
662 * ctf_enum(someproject_component, enum_name, int, field_e, arg0)
663 *
664 * * Array Sequence, printed as UTF8-encoded array of bytes *
665 * ctf_array_text(char, field_b, string, FIXED_LEN)
666 * ctf_sequence_text(char, field_c, string, size_t, strlen)
667 *
668 * * String, printed as UTF8-encoded string *
669 * ctf_string(field_e, string)
670 *
671 * * Array sequence of signed integer values *
672 * ctf_array(long, field_f, arg4, FIXED_LEN4)
673 * ctf_sequence(long, field_g, arg4, size_t, arg4_len)
674 * )
675 * )
676 *
677 * More detailed explanation:
678 *
679 * The name of the tracepoint is expressed as a tuple with the provider
680 * and name arguments.
681 *
682 * The provider and name should be a proper C99 identifier.
683 * The "provider" and "name" MUST follow these rules to ensure no
684 * namespace clash occurs:
685 *
686 * For projects (applications and libraries) for which an entity
687 * specific to the project controls the source code and thus its
688 * tracepoints (typically with a scope larger than a single company):
689 *
690 * either:
691 * project_component, event
692 * or:
693 * project, event
694 *
695 * Where "project" is the name of the project,
696 * "component" is the name of the project component (which may
697 * include several levels of sub-components, e.g.
698 * ...component_subcomponent_...) where the tracepoint is located
699 * (optional),
700 * "event" is the name of the tracepoint event.
701 *
702 * For projects issued from a single company wishing to advertise that
703 * the company controls the source code and thus the tracepoints, the
704 * "com_" prefix should be used:
705 *
706 * either:
707 * com_company_project_component, event
708 * or:
709 * com_company_project, event
710 *
711 * Where "company" is the name of the company,
712 * "project" is the name of the project,
713 * "component" is the name of the project component (which may
714 * include several levels of sub-components, e.g.
715 * ...component_subcomponent_...) where the tracepoint is located
716 * (optional),
717 * "event" is the name of the tracepoint event.
718 *
719 * the provider:event identifier is limited to 127 characters.
720 */
721
722 #define LTTNG_UST_TRACEPOINT_EVENT(provider, name, args, fields) \
723 LTTNG_UST__DECLARE_TRACEPOINT(provider, name, LTTNG_UST__TP_PARAMS(args)) \
724 LTTNG_UST__DEFINE_TRACEPOINT(provider, name, LTTNG_UST__TP_PARAMS(args))
725
726 #define LTTNG_UST_TRACEPOINT_EVENT_CLASS(provider, name, args, fields)
727
728 #define LTTNG_UST_TRACEPOINT_EVENT_INSTANCE(provider, _template, name, args) \
729 LTTNG_UST__DECLARE_TRACEPOINT(provider, name, LTTNG_UST__TP_PARAMS(args)) \
730 LTTNG_UST__DEFINE_TRACEPOINT(provider, name, LTTNG_UST__TP_PARAMS(args))
731
732 #if LTTNG_UST_COMPAT_API(0)
733 #define TRACEPOINT_EVENT LTTNG_UST_TRACEPOINT_EVENT
734 #define TRACEPOINT_EVENT_CLASS LTTNG_UST_TRACEPOINT_EVENT_CLASS
735 #define TRACEPOINT_EVENT_INSTANCE LTTNG_UST_TRACEPOINT_EVENT_INSTANCE
736 #endif /* #if LTTNG_UST_COMPAT_API(0) */
737
738 #endif /* #ifndef LTTNG_UST_TRACEPOINT_EVENT */
739
740 #ifndef TRACEPOINT_LOGLEVEL
741
742 /*
743 * Tracepoint Loglevels
744 *
745 * Typical use of these loglevels:
746 *
747 * The loglevels go from 0 to 14. Higher numbers imply the most
748 * verbosity (higher event throughput expected.
749 *
750 * Loglevels 0 through 6, and loglevel 14, match syslog(3) loglevels
751 * semantic. Loglevels 7 through 13 offer more fine-grained selection of
752 * debug information.
753 *
754 * TRACE_EMERG 0
755 * system is unusable
756 *
757 * TRACE_ALERT 1
758 * action must be taken immediately
759 *
760 * TRACE_CRIT 2
761 * critical conditions
762 *
763 * TRACE_ERR 3
764 * error conditions
765 *
766 * TRACE_WARNING 4
767 * warning conditions
768 *
769 * TRACE_NOTICE 5
770 * normal, but significant, condition
771 *
772 * TRACE_INFO 6
773 * informational message
774 *
775 * TRACE_DEBUG_SYSTEM 7
776 * debug information with system-level scope (set of programs)
777 *
778 * TRACE_DEBUG_PROGRAM 8
779 * debug information with program-level scope (set of processes)
780 *
781 * TRACE_DEBUG_PROCESS 9
782 * debug information with process-level scope (set of modules)
783 *
784 * TRACE_DEBUG_MODULE 10
785 * debug information with module (executable/library) scope (set of units)
786 *
787 * TRACE_DEBUG_UNIT 11
788 * debug information with compilation unit scope (set of functions)
789 *
790 * TRACE_DEBUG_FUNCTION 12
791 * debug information with function-level scope
792 *
793 * TRACE_DEBUG_LINE 13
794 * debug information with line-level scope (LTTNG_UST_TRACEPOINT_EVENT default)
795 *
796 * TRACE_DEBUG 14
797 * debug-level message
798 *
799 * Declare tracepoint loglevels for tracepoints. A LTTNG_UST_TRACEPOINT_EVENT
800 * should be declared prior to the the TRACEPOINT_LOGLEVEL for a given
801 * tracepoint name. The first field is the provider name, the second
802 * field is the name of the tracepoint, the third field is the loglevel
803 * name.
804 *
805 * TRACEPOINT_LOGLEVEL(< [com_company_]project[_component] >, < event >,
806 * < loglevel_name >)
807 *
808 * The TRACEPOINT_PROVIDER must be already declared before declaring a
809 * TRACEPOINT_LOGLEVEL.
810 */
811
812 enum {
813 TRACE_EMERG = 0,
814 TRACE_ALERT = 1,
815 TRACE_CRIT = 2,
816 TRACE_ERR = 3,
817 TRACE_WARNING = 4,
818 TRACE_NOTICE = 5,
819 TRACE_INFO = 6,
820 TRACE_DEBUG_SYSTEM = 7,
821 TRACE_DEBUG_PROGRAM = 8,
822 TRACE_DEBUG_PROCESS = 9,
823 TRACE_DEBUG_MODULE = 10,
824 TRACE_DEBUG_UNIT = 11,
825 TRACE_DEBUG_FUNCTION = 12,
826 TRACE_DEBUG_LINE = 13,
827 TRACE_DEBUG = 14,
828 };
829
830 #define TRACEPOINT_LOGLEVEL(provider, name, loglevel)
831
832 #endif /* #ifndef TRACEPOINT_LOGLEVEL */
833
834 #ifndef TRACEPOINT_MODEL_EMF_URI
835
836 #define TRACEPOINT_MODEL_EMF_URI(provider, name, uri)
837
838 #endif /* #ifndef TRACEPOINT_MODEL_EMF_URI */
This page took 0.048563 seconds and 5 git commands to generate.