Tracepoint API namespacing 'TRACEPOINT_EVENT'
[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(__tracepoint_##provider##___##name.state))
55
56 #define lttng_ust_do_tracepoint(provider, name, ...) \
57 __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 __TP_COMBINE_TOKENS(_tokena, _tokenb) \
75 _tokena##_tokenb
76 #define _TP_COMBINE_TOKENS(_tokena, _tokenb) \
77 __TP_COMBINE_TOKENS(_tokena, _tokenb)
78 #define __TP_COMBINE_TOKENS3(_tokena, _tokenb, _tokenc) \
79 _tokena##_tokenb##_tokenc
80 #define _TP_COMBINE_TOKENS3(_tokena, _tokenb, _tokenc) \
81 __TP_COMBINE_TOKENS3(_tokena, _tokenb, _tokenc)
82 #define __TP_COMBINE_TOKENS4(_tokena, _tokenb, _tokenc, _tokend) \
83 _tokena##_tokenb##_tokenc##_tokend
84 #define _TP_COMBINE_TOKENS4(_tokena, _tokenb, _tokenc, _tokend) \
85 __TP_COMBINE_TOKENS4(_tokena, _tokenb, _tokenc, _tokend)
86
87 /*
88 * _TP_EXVAR* extract the var names.
89 * _TP_EXVAR1 and _TP_EXDATA_VAR1 are needed for -std=c99.
90 */
91 #define _TP_EXVAR0()
92 #define _TP_EXVAR1(a)
93 #define _TP_EXVAR2(a,b) b
94 #define _TP_EXVAR4(a,b,c,d) b,d
95 #define _TP_EXVAR6(a,b,c,d,e,f) b,d,f
96 #define _TP_EXVAR8(a,b,c,d,e,f,g,h) b,d,f,h
97 #define _TP_EXVAR10(a,b,c,d,e,f,g,h,i,j) b,d,f,h,j
98 #define _TP_EXVAR12(a,b,c,d,e,f,g,h,i,j,k,l) b,d,f,h,j,l
99 #define _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 _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 _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 _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 _TP_EXDATA_VAR0() __tp_data
105 #define _TP_EXDATA_VAR1(a) __tp_data
106 #define _TP_EXDATA_VAR2(a,b) __tp_data,b
107 #define _TP_EXDATA_VAR4(a,b,c,d) __tp_data,b,d
108 #define _TP_EXDATA_VAR6(a,b,c,d,e,f) __tp_data,b,d,f
109 #define _TP_EXDATA_VAR8(a,b,c,d,e,f,g,h) __tp_data,b,d,f,h
110 #define _TP_EXDATA_VAR10(a,b,c,d,e,f,g,h,i,j) __tp_data,b,d,f,h,j
111 #define _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 _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 _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 _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 _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 * _TP_EXPROTO* extract tuples of type, var.
119 * _TP_EXPROTO1 and _TP_EXDATA_PROTO1 are needed for -std=c99.
120 */
121 #define _TP_EXPROTO0() void
122 #define _TP_EXPROTO1(a) void
123 #define _TP_EXPROTO2(a,b) a b
124 #define _TP_EXPROTO4(a,b,c,d) a b,c d
125 #define _TP_EXPROTO6(a,b,c,d,e,f) a b,c d,e f
126 #define _TP_EXPROTO8(a,b,c,d,e,f,g,h) a b,c d,e f,g h
127 #define _TP_EXPROTO10(a,b,c,d,e,f,g,h,i,j) a b,c d,e f,g h,i j
128 #define _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 _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 _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 _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 _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 _TP_EXDATA_PROTO0() void *__tp_data
135 #define _TP_EXDATA_PROTO1(a) void *__tp_data
136 #define _TP_EXDATA_PROTO2(a,b) void *__tp_data,a b
137 #define _TP_EXDATA_PROTO4(a,b,c,d) void *__tp_data,a b,c d
138 #define _TP_EXDATA_PROTO6(a,b,c,d,e,f) void *__tp_data,a b,c d,e f
139 #define _TP_EXDATA_PROTO8(a,b,c,d,e,f,g,h) void *__tp_data,a b,c d,e f,g h
140 #define _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 _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 _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 _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 _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 _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 _TP_NARGS(...) __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 __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 _TP_PROTO_N(N, ...) _TP_PARAMS(_TP_COMBINE_TOKENS(_TP_EXPROTO, N)(__VA_ARGS__))
151 #define _TP_VAR_N(N, ...) _TP_PARAMS(_TP_COMBINE_TOKENS(_TP_EXVAR, N)(__VA_ARGS__))
152 #define _TP_DATA_PROTO_N(N, ...) _TP_PARAMS(_TP_COMBINE_TOKENS(_TP_EXDATA_PROTO, N)(__VA_ARGS__))
153 #define _TP_DATA_VAR_N(N, ...) _TP_PARAMS(_TP_COMBINE_TOKENS(_TP_EXDATA_VAR, N)(__VA_ARGS__))
154 #define _TP_ARGS_PROTO(...) _TP_PROTO_N(_TP_NARGS(0, ##__VA_ARGS__), ##__VA_ARGS__)
155 #define _TP_ARGS_VAR(...) _TP_VAR_N(_TP_NARGS(0, ##__VA_ARGS__), ##__VA_ARGS__)
156 #define _TP_ARGS_DATA_PROTO(...) _TP_DATA_PROTO_N(_TP_NARGS(0, ##__VA_ARGS__), ##__VA_ARGS__)
157 #define _TP_ARGS_DATA_VAR(...) _TP_DATA_VAR_N(_TP_NARGS(0, ##__VA_ARGS__), ##__VA_ARGS__)
158 #define _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 __tracepoint_##_provider##___##_name; \
186 static inline \
187 void __tracepoint_cb_##_provider##___##_name(_TP_ARGS_PROTO(__VA_ARGS__)) \
188 __attribute__((always_inline, unused)) lttng_ust_notrace; \
189 static \
190 void __tracepoint_cb_##_provider##___##_name(_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(__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 (*)(_TP_ARGS_DATA_PROTO(__VA_ARGS__)), __tp_cb) \
205 (_TP_ARGS_DATA_VAR(__VA_ARGS__)); \
206 } while ((++__tp_probe)->func); \
207 end: \
208 tp_rcu_read_unlock(); \
209 } \
210 static inline \
211 void __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 __tracepoint_register_##_provider##___##_name(char *provider_name, char *event_name, \
216 void (*func)(void), void *data) \
217 { \
218 __tracepoint_probe_register(provider_name, event_name, func, data, \
219 __tracepoint_##_provider##___##_name.signature); \
220 } \
221 static inline \
222 void __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 __tracepoint_unregister_##_provider##___##_name(char *provider_name, char *event_name, \
227 void (*func)(void), void *data) \
228 { \
229 __tracepoint_probe_unregister(provider_name, event_name, func, data); \
230 }
231
232 extern int __tracepoint_probe_register(const char *provider_name, const char *event_name,
233 void (*func)(void), void *data, const char *signature);
234 extern int __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 tracepoint_dlopen;
265 extern struct lttng_ust_tracepoint_dlopen *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 __tracepoint_registered
273 __attribute__((weak, visibility("hidden")));
274 int __tracepoint_ptrs_registered
275 __attribute__((weak, visibility("hidden")));
276 struct lttng_ust_tracepoint_dlopen 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 *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 tracepoint_destructors_syms;
316 extern struct lttng_ust_tracepoint_destructors_syms *tracepoint_destructors_syms_ptr;
317
318 struct lttng_ust_tracepoint_destructors_syms 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 *tracepoint_destructors_syms_ptr
323 __attribute__((weak, visibility("hidden")));
324
325 static inline void tracepoint_disable_destructors(void)
326 {
327 if (!tracepoint_dlopen_ptr)
328 tracepoint_dlopen_ptr = &tracepoint_dlopen;
329 if (!tracepoint_destructors_syms_ptr)
330 tracepoint_destructors_syms_ptr = &tracepoint_destructors_syms;
331 if (tracepoint_dlopen_ptr->liblttngust_handle
332 && tracepoint_destructors_syms_ptr->tracepoint_disable_destructors)
333 tracepoint_destructors_syms_ptr->tracepoint_disable_destructors();
334 }
335
336 #ifndef _LGPL_SOURCE
337 static inline void
338 __tracepoint__init_urcu_sym(void)
339 lttng_ust_notrace;
340 static inline void
341 __tracepoint__init_urcu_sym(void)
342 {
343 if (!tracepoint_dlopen_ptr)
344 tracepoint_dlopen_ptr = &tracepoint_dlopen;
345 /*
346 * Symbols below are needed by tracepoint call sites and probe
347 * providers.
348 */
349 if (!tracepoint_dlopen_ptr->rcu_read_lock_sym)
350 tracepoint_dlopen_ptr->rcu_read_lock_sym =
351 URCU_FORCE_CAST(void (*)(void),
352 dlsym(tracepoint_dlopen_ptr->liblttngust_handle,
353 "tp_rcu_read_lock"));
354 if (!tracepoint_dlopen_ptr->rcu_read_unlock_sym)
355 tracepoint_dlopen_ptr->rcu_read_unlock_sym =
356 URCU_FORCE_CAST(void (*)(void),
357 dlsym(tracepoint_dlopen_ptr->liblttngust_handle,
358 "tp_rcu_read_unlock"));
359 if (!tracepoint_dlopen_ptr->rcu_dereference_sym)
360 tracepoint_dlopen_ptr->rcu_dereference_sym =
361 URCU_FORCE_CAST(void *(*)(void *p),
362 dlsym(tracepoint_dlopen_ptr->liblttngust_handle,
363 "tp_rcu_dereference_sym"));
364 }
365 #else
366 static inline void
367 __tracepoint__init_urcu_sym(void)
368 lttng_ust_notrace;
369 static inline void
370 __tracepoint__init_urcu_sym(void)
371 {
372 }
373 #endif
374
375 static void
376 __tracepoints__init(void)
377 lttng_ust_notrace __attribute__((constructor));
378 static void
379 __tracepoints__init(void)
380 {
381 if (__tracepoint_registered++) {
382 if (!tracepoint_dlopen_ptr->liblttngust_handle)
383 return;
384 __tracepoint__init_urcu_sym();
385 return;
386 }
387
388 if (!tracepoint_dlopen_ptr)
389 tracepoint_dlopen_ptr = &tracepoint_dlopen;
390 if (!tracepoint_dlopen_ptr->liblttngust_handle)
391 tracepoint_dlopen_ptr->liblttngust_handle =
392 dlopen(LTTNG_UST_TRACEPOINT_LIB_SONAME, RTLD_NOW | RTLD_GLOBAL);
393 if (!tracepoint_dlopen_ptr->liblttngust_handle)
394 return;
395 __tracepoint__init_urcu_sym();
396 }
397
398 static void
399 __tracepoints__destroy(void)
400 lttng_ust_notrace __attribute__((destructor));
401 static void
402 __tracepoints__destroy(void)
403 {
404 int ret;
405
406 if (--__tracepoint_registered)
407 return;
408 if (!tracepoint_dlopen_ptr)
409 tracepoint_dlopen_ptr = &tracepoint_dlopen;
410 if (!tracepoint_destructors_syms_ptr)
411 tracepoint_destructors_syms_ptr = &tracepoint_destructors_syms;
412 if (!tracepoint_dlopen_ptr->liblttngust_handle)
413 return;
414 if (__tracepoint_ptrs_registered)
415 return;
416 /*
417 * Lookup if destructors must be executed using the new method.
418 */
419 if (tracepoint_destructors_syms_ptr->tracepoint_get_destructors_state
420 && !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(tracepoint_dlopen_ptr->liblttngust_handle);
429 if (ret) {
430 fprintf(stderr, "Error (%d) in dlclose\n", ret);
431 abort();
432 }
433 memset(tracepoint_dlopen_ptr, 0, sizeof(*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___tracepoints_ptrs[]
444 __attribute__((weak, visibility("hidden")));
445 extern struct lttng_ust_tracepoint * const __stop___tracepoints_ptrs[]
446 __attribute__((weak, visibility("hidden")));
447
448 /*
449 * When TRACEPOINT_PROBE_DYNAMIC_LINKAGE is defined, we do not emit a
450 * unresolved symbol that requires the provider to be linked in. When
451 * 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 #ifdef TRACEPOINT_PROBE_DYNAMIC_LINKAGE
458 #define _TRACEPOINT_UNDEFINED_REF(provider) NULL
459 #else /* TRACEPOINT_PROBE_DYNAMIC_LINKAGE */
460 #define _TRACEPOINT_UNDEFINED_REF(provider) &__tracepoint_provider_##provider
461 #endif /* TRACEPOINT_PROBE_DYNAMIC_LINKAGE */
462
463 /*
464 * Note: to allow PIC code, we need to allow the linker to update the pointers
465 * in the __tracepoints_ptrs section.
466 * Therefore, this section is _not_ const (read-only).
467 */
468 #define _TP_EXTRACT_STRING(...) #__VA_ARGS__
469
470 #define LTTNG_UST__DEFINE_TRACEPOINT(_provider, _name, _args) \
471 lttng_ust_tracepoint_validate_name_len(_provider, _name); \
472 extern int __tracepoint_provider_##_provider; \
473 static const char __tp_provider_strtab_##_provider##___##_name[] \
474 __attribute__((section("__tracepoints_strings"))) = \
475 #_provider; \
476 static const char __tp_name_strtab_##_provider##___##_name[] \
477 __attribute__((section("__tracepoints_strings"))) = \
478 #_name; \
479 struct lttng_ust_tracepoint __tracepoint_##_provider##___##_name \
480 __attribute__((section("__tracepoints"))) = { \
481 sizeof(struct lttng_ust_tracepoint), \
482 __tp_provider_strtab_##_provider##___##_name, \
483 __tp_name_strtab_##_provider##___##_name, \
484 0, \
485 NULL, \
486 _TRACEPOINT_UNDEFINED_REF(_provider), \
487 _TP_EXTRACT_STRING(_args), \
488 }; \
489 static struct lttng_ust_tracepoint * \
490 __tracepoint_ptr_##_provider##___##_name \
491 __attribute__((section("__tracepoints_ptrs"), used)) \
492 __lttng_ust_variable_attribute_no_sanitize_address = \
493 &__tracepoint_##_provider##___##_name;
494
495 static void
496 __tracepoints__ptrs_init(void)
497 lttng_ust_notrace __attribute__((constructor));
498 static void
499 __tracepoints__ptrs_init(void)
500 {
501 if (__tracepoint_ptrs_registered++)
502 return;
503 if (!tracepoint_dlopen_ptr)
504 tracepoint_dlopen_ptr = &tracepoint_dlopen;
505 if (!tracepoint_dlopen_ptr->liblttngust_handle)
506 tracepoint_dlopen_ptr->liblttngust_handle =
507 dlopen(LTTNG_UST_TRACEPOINT_LIB_SONAME, RTLD_NOW | RTLD_GLOBAL);
508 if (!tracepoint_dlopen_ptr->liblttngust_handle)
509 return;
510 if (!tracepoint_destructors_syms_ptr)
511 tracepoint_destructors_syms_ptr = &tracepoint_destructors_syms;
512 tracepoint_dlopen_ptr->tracepoint_register_lib =
513 URCU_FORCE_CAST(int (*)(struct lttng_ust_tracepoint * const *, int),
514 dlsym(tracepoint_dlopen_ptr->liblttngust_handle,
515 "tracepoint_register_lib"));
516 tracepoint_dlopen_ptr->tracepoint_unregister_lib =
517 URCU_FORCE_CAST(int (*)(struct lttng_ust_tracepoint * const *),
518 dlsym(tracepoint_dlopen_ptr->liblttngust_handle,
519 "tracepoint_unregister_lib"));
520 tracepoint_destructors_syms_ptr->tracepoint_disable_destructors =
521 URCU_FORCE_CAST(void (*)(void),
522 dlsym(tracepoint_dlopen_ptr->liblttngust_handle,
523 "tp_disable_destructors"));
524 tracepoint_destructors_syms_ptr->tracepoint_get_destructors_state =
525 URCU_FORCE_CAST(int (*)(void),
526 dlsym(tracepoint_dlopen_ptr->liblttngust_handle,
527 "tp_get_destructors_state"));
528 __tracepoint__init_urcu_sym();
529 if (tracepoint_dlopen_ptr->tracepoint_register_lib) {
530 tracepoint_dlopen_ptr->tracepoint_register_lib(__start___tracepoints_ptrs,
531 __stop___tracepoints_ptrs -
532 __start___tracepoints_ptrs);
533 }
534 }
535
536 static void
537 __tracepoints__ptrs_destroy(void)
538 lttng_ust_notrace __attribute__((destructor));
539 static void
540 __tracepoints__ptrs_destroy(void)
541 {
542 int ret;
543
544 if (--__tracepoint_ptrs_registered)
545 return;
546 if (!tracepoint_dlopen_ptr)
547 tracepoint_dlopen_ptr = &tracepoint_dlopen;
548 if (!tracepoint_destructors_syms_ptr)
549 tracepoint_destructors_syms_ptr = &tracepoint_destructors_syms;
550 if (tracepoint_dlopen_ptr->tracepoint_unregister_lib)
551 tracepoint_dlopen_ptr->tracepoint_unregister_lib(__start___tracepoints_ptrs);
552 if (tracepoint_dlopen_ptr->liblttngust_handle
553 && tracepoint_destructors_syms_ptr->tracepoint_get_destructors_state
554 && tracepoint_destructors_syms_ptr->tracepoint_get_destructors_state()
555 && !__tracepoint_ptrs_registered) {
556 ret = dlclose(tracepoint_dlopen_ptr->liblttngust_handle);
557 if (ret) {
558 fprintf(stderr, "Error (%d) in dlclose\n", ret);
559 abort();
560 }
561 memset(tracepoint_dlopen_ptr, 0, sizeof(*tracepoint_dlopen_ptr));
562 }
563 }
564
565 #else /* TRACEPOINT_DEFINE */
566
567 #define LTTNG_UST__DEFINE_TRACEPOINT(_provider, _name, _args)
568
569 #endif /* #else TRACEPOINT_DEFINE */
570
571 #if LTTNG_UST_COMPAT_API(0)
572 #define tracepoint lttng_ust_tracepoint
573 #define do_tracepoint lttng_ust_do_tracepoint
574 #define tracepoint_enabled lttng_ust_tracepoint_enabled
575 #define TP_ARGS LTTNG_UST_TP_ARGS
576 #define TP_FIELDS LTTNG_UST_TP_FIELDS
577 #endif /* #if LTTNG_UST_COMPAT_API(0) */
578
579 #ifdef __cplusplus
580 }
581 #endif
582
583 #endif /* _LTTNG_TRACEPOINT_H */
584
585 /* The following declarations must be outside re-inclusion protection. */
586
587 #ifndef TRACEPOINT_ENUM
588
589 /*
590 * Tracepoint Enumerations
591 *
592 * The enumeration is a mapping between an integer, or range of integers, and
593 * a string. It can be used to have a more compact trace in cases where the
594 * possible values for a field are limited:
595 *
596 * An example:
597 *
598 * TRACEPOINT_ENUM(someproject_component, enumname,
599 * TP_ENUM_VALUES(
600 * ctf_enum_value("even", 0)
601 * ctf_enum_value("uneven", 1)
602 * ctf_enum_range("twoto4", 2, 4)
603 * ctf_enum_value("five", 5)
604 * )
605 * )
606 *
607 * Where "someproject_component" is the name of the component this enumeration
608 * belongs to and "enumname" identifies this enumeration. Inside the
609 * TP_ENUM_VALUES macro is the actual mapping. Each string value can map
610 * to either a single value with ctf_enum_value or a range of values
611 * with ctf_enum_range.
612 *
613 * Enumeration ranges may overlap, but the behavior is implementation-defined,
614 * each trace reader will handle overlapping as it wishes.
615 *
616 * That enumeration can then be used in a field inside the TP_FIELD macro using
617 * the following line:
618 *
619 * ctf_enum(someproject_component, enumname, enumtype, enumfield, enumval)
620 *
621 * Where "someproject_component" and "enumname" match those in the
622 * TRACEPOINT_ENUM, "enumtype" is a signed or unsigned integer type
623 * backing the enumeration, "enumfield" is the name of the field and
624 * "enumval" is the value.
625 */
626
627 #define TRACEPOINT_ENUM(provider, name, values)
628
629 #endif /* #ifndef TRACEPOINT_ENUM */
630
631 #ifndef LTTNG_UST_TRACEPOINT_EVENT
632
633 /*
634 * How to use the LTTNG_UST_TRACEPOINT_EVENT macro:
635 *
636 * An example:
637 *
638 * LTTNG_UST_TRACEPOINT_EVENT(someproject_component, event_name,
639 *
640 * * LTTNG_UST_TP_ARGS takes from 0 to 10 "type, field_name" pairs *
641 *
642 * LTTNG_UST_TP_ARGS(int, arg0, void *, arg1, char *, string, size_t, strlen,
643 * long *, arg4, size_t, arg4_len),
644 *
645 * * LTTNG_UST_TP_FIELDS describes the event payload layout in the trace *
646 *
647 * LTTNG_UST_TP_FIELDS(
648 * * Integer, printed in base 10 *
649 * ctf_integer(int, field_a, arg0)
650 *
651 * * Integer, printed with 0x base 16 *
652 * ctf_integer_hex(unsigned long, field_d, arg1)
653 *
654 * * Enumeration *
655 * ctf_enum(someproject_component, enum_name, int, field_e, arg0)
656 *
657 * * Array Sequence, printed as UTF8-encoded array of bytes *
658 * ctf_array_text(char, field_b, string, FIXED_LEN)
659 * ctf_sequence_text(char, field_c, string, size_t, strlen)
660 *
661 * * String, printed as UTF8-encoded string *
662 * ctf_string(field_e, string)
663 *
664 * * Array sequence of signed integer values *
665 * ctf_array(long, field_f, arg4, FIXED_LEN4)
666 * ctf_sequence(long, field_g, arg4, size_t, arg4_len)
667 * )
668 * )
669 *
670 * More detailed explanation:
671 *
672 * The name of the tracepoint is expressed as a tuple with the provider
673 * and name arguments.
674 *
675 * The provider and name should be a proper C99 identifier.
676 * The "provider" and "name" MUST follow these rules to ensure no
677 * namespace clash occurs:
678 *
679 * For projects (applications and libraries) for which an entity
680 * specific to the project controls the source code and thus its
681 * tracepoints (typically with a scope larger than a single company):
682 *
683 * either:
684 * project_component, event
685 * or:
686 * project, event
687 *
688 * Where "project" is the name of the project,
689 * "component" is the name of the project component (which may
690 * include several levels of sub-components, e.g.
691 * ...component_subcomponent_...) where the tracepoint is located
692 * (optional),
693 * "event" is the name of the tracepoint event.
694 *
695 * For projects issued from a single company wishing to advertise that
696 * the company controls the source code and thus the tracepoints, the
697 * "com_" prefix should be used:
698 *
699 * either:
700 * com_company_project_component, event
701 * or:
702 * com_company_project, event
703 *
704 * Where "company" is the name of the company,
705 * "project" is the name of the project,
706 * "component" is the name of the project component (which may
707 * include several levels of sub-components, e.g.
708 * ...component_subcomponent_...) where the tracepoint is located
709 * (optional),
710 * "event" is the name of the tracepoint event.
711 *
712 * the provider:event identifier is limited to 127 characters.
713 */
714
715 #define LTTNG_UST_TRACEPOINT_EVENT(provider, name, args, fields) \
716 LTTNG_UST__DECLARE_TRACEPOINT(provider, name, _TP_PARAMS(args)) \
717 LTTNG_UST__DEFINE_TRACEPOINT(provider, name, _TP_PARAMS(args))
718
719 #define LTTNG_UST_TRACEPOINT_EVENT_CLASS(provider, name, args, fields)
720
721 #define LTTNG_UST_TRACEPOINT_EVENT_INSTANCE(provider, _template, name, args) \
722 LTTNG_UST__DECLARE_TRACEPOINT(provider, name, _TP_PARAMS(args)) \
723 LTTNG_UST__DEFINE_TRACEPOINT(provider, name, _TP_PARAMS(args))
724
725 #if LTTNG_UST_COMPAT_API(0)
726 #define TRACEPOINT_EVENT LTTNG_UST_TRACEPOINT_EVENT
727 #define TRACEPOINT_EVENT_CLASS LTTNG_UST_TRACEPOINT_EVENT_CLASS
728 #define TRACEPOINT_EVENT_INSTANCE LTTNG_UST_TRACEPOINT_EVENT_INSTANCE
729 #endif /* #if LTTNG_UST_COMPAT_API(0) */
730
731 #endif /* #ifndef LTTNG_UST_TRACEPOINT_EVENT */
732
733 #ifndef TRACEPOINT_LOGLEVEL
734
735 /*
736 * Tracepoint Loglevels
737 *
738 * Typical use of these loglevels:
739 *
740 * The loglevels go from 0 to 14. Higher numbers imply the most
741 * verbosity (higher event throughput expected.
742 *
743 * Loglevels 0 through 6, and loglevel 14, match syslog(3) loglevels
744 * semantic. Loglevels 7 through 13 offer more fine-grained selection of
745 * debug information.
746 *
747 * TRACE_EMERG 0
748 * system is unusable
749 *
750 * TRACE_ALERT 1
751 * action must be taken immediately
752 *
753 * TRACE_CRIT 2
754 * critical conditions
755 *
756 * TRACE_ERR 3
757 * error conditions
758 *
759 * TRACE_WARNING 4
760 * warning conditions
761 *
762 * TRACE_NOTICE 5
763 * normal, but significant, condition
764 *
765 * TRACE_INFO 6
766 * informational message
767 *
768 * TRACE_DEBUG_SYSTEM 7
769 * debug information with system-level scope (set of programs)
770 *
771 * TRACE_DEBUG_PROGRAM 8
772 * debug information with program-level scope (set of processes)
773 *
774 * TRACE_DEBUG_PROCESS 9
775 * debug information with process-level scope (set of modules)
776 *
777 * TRACE_DEBUG_MODULE 10
778 * debug information with module (executable/library) scope (set of units)
779 *
780 * TRACE_DEBUG_UNIT 11
781 * debug information with compilation unit scope (set of functions)
782 *
783 * TRACE_DEBUG_FUNCTION 12
784 * debug information with function-level scope
785 *
786 * TRACE_DEBUG_LINE 13
787 * debug information with line-level scope (LTTNG_UST_TRACEPOINT_EVENT default)
788 *
789 * TRACE_DEBUG 14
790 * debug-level message
791 *
792 * Declare tracepoint loglevels for tracepoints. A LTTNG_UST_TRACEPOINT_EVENT
793 * should be declared prior to the the TRACEPOINT_LOGLEVEL for a given
794 * tracepoint name. The first field is the provider name, the second
795 * field is the name of the tracepoint, the third field is the loglevel
796 * name.
797 *
798 * TRACEPOINT_LOGLEVEL(< [com_company_]project[_component] >, < event >,
799 * < loglevel_name >)
800 *
801 * The TRACEPOINT_PROVIDER must be already declared before declaring a
802 * TRACEPOINT_LOGLEVEL.
803 */
804
805 enum {
806 TRACE_EMERG = 0,
807 TRACE_ALERT = 1,
808 TRACE_CRIT = 2,
809 TRACE_ERR = 3,
810 TRACE_WARNING = 4,
811 TRACE_NOTICE = 5,
812 TRACE_INFO = 6,
813 TRACE_DEBUG_SYSTEM = 7,
814 TRACE_DEBUG_PROGRAM = 8,
815 TRACE_DEBUG_PROCESS = 9,
816 TRACE_DEBUG_MODULE = 10,
817 TRACE_DEBUG_UNIT = 11,
818 TRACE_DEBUG_FUNCTION = 12,
819 TRACE_DEBUG_LINE = 13,
820 TRACE_DEBUG = 14,
821 };
822
823 #define TRACEPOINT_LOGLEVEL(provider, name, loglevel)
824
825 #endif /* #ifndef TRACEPOINT_LOGLEVEL */
826
827 #ifndef TRACEPOINT_MODEL_EMF_URI
828
829 #define TRACEPOINT_MODEL_EMF_URI(provider, name, uri)
830
831 #endif /* #ifndef TRACEPOINT_MODEL_EMF_URI */
This page took 0.046904 seconds and 5 git commands to generate.