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