tracepoint: support TP_ARGS(void)
[lttng-ust.git] / include / lttng / tracepoint.h
1 #ifndef _LTTNG_TRACEPOINT_H
2 #define _LTTNG_TRACEPOINT_H
3
4 /*
5 * Copyright (C) 2008-2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Copyright (C) 2009 Pierre-Marc Fournier
7 * Copyright (C) 2009 Steven Rostedt <rostedt@goodmis.org>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation;
12 * version 2.1 of the License.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 * Heavily inspired from the Linux Kernel Markers.
24 */
25
26 #include <urcu-bp.h>
27 #include <urcu/list.h>
28 #include <lttng/tracepoint-types.h>
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 /*
35 * Tracepoints should be added to the instrumented code using the
36 * "tracepoint()" macro.
37 */
38 #define tracepoint(provider, name, args...) \
39 __trace_##provider##___##name(args)
40
41 /*
42 * it_func[0] is never NULL because there is at least one element in the array
43 * when the array itself is non NULL.
44 */
45 #define __DO_TRACE(tp, proto, vars) \
46 do { \
47 struct tracepoint_probe *__tp_it_probe_ptr; \
48 void *__tp_it_func; \
49 void *__tp_cb_data; \
50 \
51 rcu_read_lock(); \
52 __tp_it_probe_ptr = rcu_dereference((tp)->probes); \
53 if (__tp_it_probe_ptr) { \
54 do { \
55 __tp_it_func = __tp_it_probe_ptr->func; \
56 __tp_cb_data = __tp_it_probe_ptr->data; \
57 URCU_FORCE_CAST(void(*)(proto), __tp_it_func)(vars); \
58 } while ((++__tp_it_probe_ptr)->func); \
59 } \
60 rcu_read_unlock(); \
61 } while (0)
62
63 #define TP_PARAMS(args...) args
64 #define TP_ARGS(args...) args
65
66 /*
67 * TP_ARGS takes tuples of type, argument separated by a comma. It can
68 * take up to 10 tuples (which means that less than 10 tuples is fine
69 * too). Each tuple is also separated by a comma.
70 */
71
72 #define TP_COMBINE_TOKENS1(_tokena, _tokenb) _tokena##_tokenb
73 #define TP_COMBINE_TOKENS(_tokena, _tokenb) TP_COMBINE_TOKENS1(_tokena, _tokenb)
74
75 /* _TP_EVEN* extracts the vars names. */
76 /* 0 and 1 are for void */
77 #define _TP_EVEN0()
78 #define _TP_EVEN1(a)
79 #define _TP_EVEN2(a,b) b
80 #define _TP_EVEN4(a,b,c,d) b,d
81 #define _TP_EVEN6(a,b,c,d,e,f) b,d,f
82 #define _TP_EVEN8(a,b,c,d,e,f,g,h) b,d,f,h
83 #define _TP_EVEN10(a,b,c,d,e,f,g,h,i,j) b,d,f,h,j
84 #define _TP_EVEN12(a,b,c,d,e,f,g,h,i,j,k,l) b,d,f,h,j,l
85 #define _TP_EVEN14(a,b,c,d,e,f,g,h,i,j,k,l,m,n) b,d,f,h,j,l,n
86 #define _TP_EVEN16(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) b,d,f,h,j,l,n,p
87 #define _TP_EVEN18(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
88 #define _TP_EVEN20(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
89
90 /* 0 and 1 are for void */
91 #define _TP_EVEN_DATA0() __tp_cb_data
92 #define _TP_EVEN_DATA1(a) __tp_cb_data
93 #define _TP_EVEN_DATA2(a,b) __tp_cb_data,b
94 #define _TP_EVEN_DATA4(a,b,c,d) __tp_cb_data,b,d
95 #define _TP_EVEN_DATA6(a,b,c,d,e,f) __tp_cb_data,b,d,f
96 #define _TP_EVEN_DATA8(a,b,c,d,e,f,g,h) __tp_cb_data,b,d,f,h
97 #define _TP_EVEN_DATA10(a,b,c,d,e,f,g,h,i,j) __tp_cb_data,b,d,f,h,j
98 #define _TP_EVEN_DATA12(a,b,c,d,e,f,g,h,i,j,k,l) __tp_cb_data,b,d,f,h,j,l
99 #define _TP_EVEN_DATA14(a,b,c,d,e,f,g,h,i,j,k,l,m,n) __tp_cb_data,b,d,f,h,j,l,n
100 #define _TP_EVEN_DATA16(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) __tp_cb_data,b,d,f,h,j,l,n,p
101 #define _TP_EVEN_DATA18(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r) __tp_cb_data,b,d,f,h,j,l,n,p,r
102 #define _TP_EVEN_DATA20(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t) __tp_cb_data,b,d,f,h,j,l,n,p,r,t
103
104 /* _TP_SPLIT extracts tuples of type, var */
105 /* 0 and 1 are for void */
106 #define _TP_SPLIT0()
107 #define _TP_SPLIT1(a)
108 #define _TP_SPLIT2(a,b) a b
109 #define _TP_SPLIT4(a,b,c,d) a b,c d
110 #define _TP_SPLIT6(a,b,c,d,e,f) a b,c d,e f
111 #define _TP_SPLIT8(a,b,c,d,e,f,g,h) a b,c d,e f,g h
112 #define _TP_SPLIT10(a,b,c,d,e,f,g,h,i,j) a b,c d,e f,g h,i j
113 #define _TP_SPLIT12(a,b,c,d,e,f,g,h,i,j,k,l) a b,c d,e f,g h,i j,k l
114 #define _TP_SPLIT14(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
115 #define _TP_SPLIT16(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
116 #define _TP_SPLIT18(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
117 #define _TP_SPLIT20(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
118
119 /* 0 and 1 are for void */
120 #define _TP_SPLIT_DATA0() void *__tp_cb_data
121 #define _TP_SPLIT_DATA1(a) void *__tp_cb_data
122 #define _TP_SPLIT_DATA2(a,b) void *__tp_cb_data,a b
123 #define _TP_SPLIT_DATA4(a,b,c,d) void *__tp_cb_data,a b,c d
124 #define _TP_SPLIT_DATA6(a,b,c,d,e,f) void *__tp_cb_data,a b,c d,e f
125 #define _TP_SPLIT_DATA8(a,b,c,d,e,f,g,h) void *__tp_cb_data,a b,c d,e f,g h
126 #define _TP_SPLIT_DATA10(a,b,c,d,e,f,g,h,i,j) void *__tp_cb_data,a b,c d,e f,g h,i j
127 #define _TP_SPLIT_DATA12(a,b,c,d,e,f,g,h,i,j,k,l) void *__tp_cb_data,a b,c d,e f,g h,i j,k l
128 #define _TP_SPLIT_DATA14(a,b,c,d,e,f,g,h,i,j,k,l,m,n) void *__tp_cb_data,a b,c d,e f,g h,i j,k l,m n
129 #define _TP_SPLIT_DATA16(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) void *__tp_cb_data,a b,c d,e f,g h,i j,k l,m n,o p
130 #define _TP_SPLIT_DATA18(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r) void *__tp_cb_data,a b,c d,e f,g h,i j,k l,m n,o p,q r
131 #define _TP_SPLIT_DATA20(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t) void *__tp_cb_data,a b,c d,e f,g h,i j,k l,m n,o p,q r,s t
132
133 /* Preprocessor trick to count arguments. Inspired from sdt.h. */
134 #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)
135 #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
136 #define _TP_PROTO_N(N, ...) \
137 TP_PARAMS(TP_COMBINE_TOKENS(_TP_SPLIT, N)(__VA_ARGS__))
138 #define _TP_VARS_N(N, ...) \
139 TP_PARAMS(TP_COMBINE_TOKENS(_TP_EVEN, N)(__VA_ARGS__))
140 #define _TP_PROTO_DATA_N(N, ...) \
141 TP_PARAMS(TP_COMBINE_TOKENS(_TP_SPLIT_DATA, N)(__VA_ARGS__))
142 #define _TP_VARS_DATA_N(N, ...) \
143 TP_PARAMS(TP_COMBINE_TOKENS(_TP_EVEN_DATA, N)(__VA_ARGS__))
144
145 #define _TP_ARGS_PROTO(...) _TP_PROTO_N(_TP_NARGS(0, ##__VA_ARGS__), ##__VA_ARGS__)
146 #define _TP_ARGS_VARS(...) _TP_VARS_N(_TP_NARGS(0, ##__VA_ARGS__), ##__VA_ARGS__)
147
148 #define _TP_ARGS_PROTO_DATA(...) _TP_PROTO_DATA_N(_TP_NARGS(0, ##__VA_ARGS__), ##__VA_ARGS__)
149 #define _TP_ARGS_VARS_DATA(...) _TP_VARS_DATA_N(_TP_NARGS(0, ##__VA_ARGS__), ##__VA_ARGS__)
150
151 #define __CHECK_TRACE(provider, name, proto, args) \
152 do { \
153 if (caa_unlikely(__tracepoint_##provider##___##name.state)) \
154 __DO_TRACE(&__tracepoint_##provider##___##name, \
155 TP_PARAMS(proto), TP_PARAMS(args)); \
156 } while (0)
157
158 /*
159 * Make sure the alignment of the structure in the __tracepoints section will
160 * not add unwanted padding between the beginning of the section and the
161 * structure. Force alignment to the same alignment as the section start.
162 */
163 #define __DECLARE_TRACEPOINT(provider, name, proto, args, data_proto, data_args) \
164 extern struct tracepoint __tracepoint_##provider##___##name; \
165 static inline void __trace_##provider##___##name(proto) \
166 { \
167 __CHECK_TRACE(provider, name, TP_PARAMS(data_proto), \
168 TP_PARAMS(data_args)); \
169 } \
170 static inline int \
171 __register_trace_##provider##___##name(void (*probe)(data_proto), void *data) \
172 { \
173 return __tracepoint_probe_register(#provider ":" #name, (void *) probe, \
174 data); \
175 } \
176 static inline int \
177 __unregister_trace_##provider##___##name(void (*probe)(data_proto), void *data) \
178 { \
179 return __tracepoint_probe_unregister(#provider ":" #name, (void *) probe, \
180 data); \
181 }
182
183 #define _DECLARE_TRACEPOINT(provider, name, args) \
184 __DECLARE_TRACEPOINT(provider, name, _TP_ARGS_PROTO(args), _TP_ARGS_VARS(args), \
185 _TP_ARGS_PROTO_DATA(args), _TP_ARGS_VARS_DATA(args))
186
187 /*
188 * __tracepoints_ptrs section is not const (read-only) to let the linker update
189 * the pointer, allowing PIC code.
190 */
191 #define _DEFINE_TRACEPOINT(provider, name) \
192 static const char __tpstrtab_##provider##___##name[] \
193 __attribute__((section("__tracepoints_strings"))) = \
194 #provider ":" #name; \
195 struct tracepoint __tracepoint_##provider##___##name \
196 __attribute__((section("__tracepoints"))) = \
197 { __tpstrtab_##provider##___##name, 0, NULL }; \
198 static struct tracepoint * __tracepoint_ptr_##provider##___##name \
199 __attribute__((used, section("__tracepoints_ptrs"))) = \
200 &__tracepoint_##provider##___##name;
201
202
203 #define __register_tracepoint(provider, name, probe, data) \
204 __register_trace_##provider##___##name(probe, data)
205 #define __unregister_tracepoint(provider, name, probe, data) \
206 __unregister_trace_##provider##___##name(probe, data)
207
208 /*
209 * Connect a probe to a tracepoint.
210 * Internal API.
211 */
212 extern
213 int __tracepoint_probe_register(const char *name, void *probe, void *data);
214
215 /*
216 * Disconnect a probe from a tracepoint.
217 * Internal API.
218 */
219 extern
220 int __tracepoint_probe_unregister(const char *name, void *probe, void *data);
221
222 extern
223 int tracepoint_register_lib(struct tracepoint * const *tracepoints_start,
224 int tracepoints_count);
225 extern
226 int tracepoint_unregister_lib(struct tracepoint * const *tracepoints_start);
227
228 /*
229 * These weak symbols, the constructor, and destructor take care of
230 * registering only _one_ instance of the tracepoints per shared-ojbect
231 * (or for the whole main program).
232 */
233 extern struct tracepoint * const __start___tracepoints_ptrs[]
234 __attribute__((weak, visibility("hidden")));
235 extern struct tracepoint * const __stop___tracepoints_ptrs[]
236 __attribute__((weak, visibility("hidden")));
237 int __tracepoint_registered
238 __attribute__((weak, visibility("hidden")));
239
240 static void __attribute__((constructor)) __tracepoints__init(void)
241 {
242 if (__tracepoint_registered++)
243 return;
244 tracepoint_register_lib(__start___tracepoints_ptrs,
245 __stop___tracepoints_ptrs -
246 __start___tracepoints_ptrs);
247 }
248
249 static void __attribute__((destructor)) __tracepoints__destroy(void)
250 {
251 if (--__tracepoint_registered)
252 return;
253 tracepoint_unregister_lib(__start___tracepoints_ptrs);
254 }
255
256 #ifdef __cplusplus
257 }
258 #endif
259
260 #endif /* _LTTNG_TRACEPOINT_H */
261
262 /*
263 * For multiple probe generation, we need to redefine TRACEPOINT_EVENT
264 * each time tracepoint.h is included.
265 */
266
267 #ifndef TRACEPOINT_EVENT
268 /*
269 * Usage of the TRACEPOINT_EVENT macro:
270 *
271 * In short, an example:
272 *
273 * TRACEPOINT_EVENT(< [com_company_]project[_component] >, < event >,
274 *
275 * * TP_ARGS takes from 0 to 10 "type, field_name" pairs *
276 *
277 * TP_ARGS(int, arg0, void *, arg1, char *, string, size_t, strlen,
278 * long *, arg4, size_t, arg4_len),
279 * TP_FIELDS(
280 *
281 * * Integer, printed in base 10 *
282 * ctf_integer(int, field_a, arg0)
283 *
284 * * Integer, printed with 0x base 16 *
285 * ctf_integer_hex(unsigned long, field_d, arg1)
286 *
287 * * Array Sequence, printed as UTF8-encoded array of bytes *
288 * ctf_array_text(char, field_b, string, FIXED_LEN)
289 * ctf_sequence_text(char, field_c, string, size_t, strlen)
290 *
291 * * String, printed as UTF8-encoded string *
292 * ctf_string(field_e, string)
293 *
294 * * Array sequence of signed integer values *
295 * ctf_array(long, field_f, arg4, FIXED_LEN4)
296 * ctf_sequence(long, field_g, arg4, size_t, arg4_len)
297 * )
298 * )
299 *
300 * In more detail:
301 *
302 * We define a tracepoint, its arguments, and its 'fast binary record'
303 * layout.
304 *
305 * Firstly, name your tracepoint via TRACEPOINT_EVENT(provider, name,
306 *
307 * The provider and name should be a proper C99 identifier.
308 * The "provider" and "name" MUST follow these rules to ensure no
309 * namespace clash occurs:
310 *
311 * For projects (applications and libraries) for which an entity
312 * specific to the project controls the source code and thus its
313 * tracepoints (typically with a scope larger than a single company):
314 *
315 * either:
316 * project_component, event
317 * or:
318 * project, event
319 *
320 * Where "project" is the name of the project,
321 * "component" is the name of the project component (which may
322 * include several levels of sub-components, e.g.
323 * ...component_subcomponent_...) where the tracepoint is located
324 * (optional),
325 * "event" is the name of the tracepoint event.
326 *
327 * For projects issued from a single company wishing to advertise that
328 * the company controls the source code and thus the tracepoints, the
329 * "com_" prefix should be used:
330 *
331 * either:
332 * com_company_project_component, event
333 * or:
334 * com_company_project, event
335 *
336 * Where "company" is the name of the company,
337 * "project" is the name of the project,
338 * "component" is the name of the project component (which may
339 * include several levels of sub-components, e.g.
340 * ...component_subcomponent_...) where the tracepoint is located
341 * (optional),
342 * "event" is the name of the tracepoint event.
343 *
344 * the provider:event identifier is limited to 127 characters.
345 *
346 * As an example, let's consider a user-space application "someproject"
347 * that would have an internal thread scheduler:
348 *
349 * TRACEPOINT_EVENT(someproject_sched, switch,
350 *
351 * *
352 * * Arguments to pass to the tracepoint. Supports from
353 * * 0 to 10 "type, name" tuples.
354 * *
355 *
356 * TP_ARGS(struct rq *, rq, struct task_struct *, prev,
357 * struct task_struct *, next),
358 *
359 * *
360 * * Fast binary tracing: define the trace record via
361 * * TP_FIELDS(). You can think about it like a
362 * * regular C structure local variable definition.
363 * *
364 * * This is how the trace record is structured and will
365 * * be saved into the ring buffer. These are the fields
366 * * that will be exposed to readers.
367 * *
368 * * ctf_integer(pid_t, prev_pid, prev->pid) is equivalent
369 * * to a standard declaraton:
370 * *
371 * * pid_t prev_pid;
372 * *
373 * * followed by an assignment:
374 * *
375 * * prev_pid = prev->pid;
376 * *
377 * * ctf_array(char, prev_comm, prev->comm, TASK_COMM_LEN) is
378 * * equivalent to:
379 * *
380 * * char prev_comm[TASK_COMM_LEN];
381 * *
382 * * followed by an assignment:
383 * *
384 * * memcpy(prev_comm, prev->comm, TASK_COMM_LEN);
385 * *
386 *
387 * TP_FIELDS(
388 * ctf_array(char, prev_comm, prev->comm, TASK_COMM_LEN)
389 * ctf_integer(pid_t, prev_pid, prev->pid)
390 * ctf_integer(int, prev_prio, prev->prio)
391 * ctf_array(char, next_comm, next->comm, TASK_COMM_LEN)
392 * ctf_integer(pid_t, next_pid, next->pid)
393 * ctf_integer(int, next_prio, next->prio)
394 * )
395 * )
396 *
397 * Do _NOT_ add comma (,) nor semicolon (;) after the TRACEPOINT_EVENT
398 * declaration.
399 *
400 * The TRACEPOINT_PROVIDER must be defined when declaring a
401 * TRACEPOINT_EVENT. See ust/tracepoint-event.h for information about
402 * usage of other macros controlling TRACEPOINT_EVENT.
403 */
404
405 #define TRACEPOINT_EVENT(provider, name, args, fields) \
406 _DECLARE_TRACEPOINT(provider, name, TP_PARAMS(args))
407
408 #define TRACEPOINT_EVENT_CLASS(provider, name, args, fields)
409 #define TRACEPOINT_EVENT_INSTANCE(provider, template, name, args)\
410 _DECLARE_TRACEPOINT(provider, name, TP_PARAMS(args))
411
412 #endif /* #ifndef TRACEPOINT_EVENT */
413
414 #ifndef TRACEPOINT_LOGLEVEL
415
416 /*
417 * Tracepoint Loglevel Declaration Facility
418 *
419 * This is a place-holder the tracepoint loglevel declaration,
420 * overridden by the tracer implementation.
421 *
422 * Typical use of these loglevels:
423 *
424 * 1) Declare the mapping between loglevel names and an integer values
425 * within TRACEPOINT_LOGLEVEL_ENUM, using TP_LOGLEVEL for each tuple.
426 * Do _NOT_ add comma (,) nor semicolon (;) between the
427 * TRACEPOINT_LOGLEVEL_ENUM entries. Do _NOT_ add comma (,) nor
428 * semicolon (;) after the TRACEPOINT_LOGLEVEL_ENUM declaration. The
429 * name should be a proper C99 identifier.
430 *
431 * TRACEPOINT_LOGLEVEL_ENUM(
432 * TP_LOGLEVEL( < loglevel_name >, < value > )
433 * TP_LOGLEVEL( < loglevel_name >, < value > )
434 * ...
435 * )
436 *
437 * e.g.:
438 *
439 * TRACEPOINT_LOGLEVEL_ENUM(
440 * TP_LOGLEVEL(LOG_EMERG, 0)
441 * TP_LOGLEVEL(LOG_ALERT, 1)
442 * TP_LOGLEVEL(LOG_CRIT, 2)
443 * TP_LOGLEVEL(LOG_ERR, 3)
444 * TP_LOGLEVEL(LOG_WARNING, 4)
445 * TP_LOGLEVEL(LOG_NOTICE, 5)
446 * TP_LOGLEVEL(LOG_INFO, 6)
447 * TP_LOGLEVEL(LOG_DEBUG, 7)
448 * )
449 *
450 * 2) Then, declare tracepoint loglevels for tracepoints. A
451 * TRACEPOINT_EVENT should be declared prior to the the
452 * TRACEPOINT_LOGLEVEL for a given tracepoint name. The first field
453 * is the name of the tracepoint, the second field is the loglevel
454 * name.
455 *
456 * TRACEPOINT_LOGLEVEL(< [com_company_]project[_component] >, < event >,
457 * < loglevel_name >)
458 *
459 * The TRACEPOINT_PROVIDER must be defined when declaring a
460 * TRACEPOINT_LOGLEVEL_ENUM and TRACEPOINT_LOGLEVEL. The tracepoint
461 * loglevel enumeration apply to the entire TRACEPOINT_PROVIDER. Only one
462 * tracepoint loglevel enumeration should be declared per tracepoint
463 * provider.
464 */
465
466 #define TRACEPOINT_LOGLEVEL_ENUM()
467 #define TRACEPOINT_LOGLEVEL(name, loglevel)
468
469 #endif /* #ifndef TRACEPOINT_LOGLEVEL */
This page took 0.0485 seconds and 5 git commands to generate.