Fix C99 strict compatibility: don't use void * for function pointers
[lttng-ust.git] / include / lttng / ust-tracepoint-event.h
1 /*
2 * Copyright (c) 2011-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 */
14
15 #include <stdio.h>
16 #include <urcu/compiler.h>
17 #include <lttng/ust-events.h>
18 #include <lttng/ringbuffer-config.h>
19 #include <string.h>
20
21 /*
22 * Macro declarations used for all stages.
23 */
24
25 #undef ctf_integer
26 #define ctf_integer(_type, _item, _src) \
27 ctf_integer_ext(_type, _item, _src, BYTE_ORDER, 10)
28
29 #undef ctf_integer_hex
30 #define ctf_integer_hex(_type, _item, _src) \
31 ctf_integer_ext(_type, _item, _src, BYTE_ORDER, 16)
32
33 #undef ctf_integer_network
34 #define ctf_integer_network(_type, _item, _src) \
35 ctf_integer_ext(_type, _item, _src, BIG_ENDIAN, 10)
36
37 #undef ctf_integer_network_hex
38 #define ctf_integer_network_hex(_type, _item, _src) \
39 ctf_integer_ext(_type, _item, _src, BIG_ENDIAN, 16)
40
41 /* ctf_float is redefined at each step */
42
43 #undef ctf_array
44 #define ctf_array(_type, _item, _src, _length) \
45 ctf_array_encoded(_type, _item, _src, _length, none)
46
47 #undef ctf_array_text
48 #define ctf_array_text(_type, _item, _src, _length) \
49 ctf_array_encoded(_type, _item, _src, _length, UTF8)
50
51 #undef ctf_sequence
52 #define ctf_sequence(_type, _item, _src, _length_type, _src_length) \
53 ctf_sequence_encoded(_type, _item, _src, \
54 _length_type, _src_length, none)
55
56 #undef ctf_sequence_text
57 #define ctf_sequence_text(_type, _item, _src, _length_type, _src_length) \
58 ctf_sequence_encoded(_type, _item, _src, \
59 _length_type, _src_length, UTF8)
60
61 /* ctf_string is redefined at each step */
62
63 /*
64 * TRACEPOINT_EVENT_CLASS declares a class of tracepoints receiving the
65 * same arguments and having the same field layout.
66 *
67 * TRACEPOINT_EVENT_INSTANCE declares an instance of a tracepoint, with
68 * its own provider and name. It refers to a class (template).
69 *
70 * TRACEPOINT_EVENT declared both a class and an instance and does a
71 * direct mapping from the instance to the class.
72 */
73
74 #undef TRACEPOINT_EVENT
75 #define TRACEPOINT_EVENT(_provider, _name, _args, _fields) \
76 TRACEPOINT_EVENT_CLASS(_provider, _name, \
77 _TP_PARAMS(_args), \
78 _TP_PARAMS(_fields)) \
79 TRACEPOINT_EVENT_INSTANCE(_provider, _name, _name, \
80 _TP_PARAMS(_args))
81
82 /* Helpers */
83 #define _TP_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
84
85 #define _tp_max_t(type, x, y) \
86 ({ \
87 type __max1 = (x); \
88 type __max2 = (y); \
89 __max1 > __max2 ? __max1: __max2; \
90 })
91
92 /*
93 * Stage 0 of tracepoint event generation.
94 *
95 * Check that each TRACEPOINT_EVENT provider argument match the
96 * TRACEPOINT_PROVIDER by creating dummy callbacks.
97 */
98
99 /* Reset all macros within TRACEPOINT_EVENT */
100 #include <lttng/ust-tracepoint-event-reset.h>
101
102 static inline
103 void _TP_COMBINE_TOKENS(__tracepoint_provider_mismatch_, TRACEPOINT_PROVIDER)(void)
104 {
105 }
106
107 #undef TRACEPOINT_EVENT_CLASS
108 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
109 __tracepoint_provider_mismatch_##_provider();
110
111 #undef TRACEPOINT_EVENT_INSTANCE
112 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
113 __tracepoint_provider_mismatch_##_provider();
114
115 static __attribute__((unused))
116 void _TP_COMBINE_TOKENS(__tracepoint_provider_check_, TRACEPOINT_PROVIDER)(void)
117 {
118 #include TRACEPOINT_INCLUDE
119 }
120
121 /*
122 * Stage 0.1 of tracepoint event generation.
123 *
124 * Check that each TRACEPOINT_EVENT provider:name does not exceed the
125 * tracepoint name length limit.
126 */
127
128 /* Reset all macros within TRACEPOINT_EVENT */
129 #include <lttng/ust-tracepoint-event-reset.h>
130
131 #undef TRACEPOINT_EVENT_INSTANCE
132 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
133 static const char \
134 __tp_name_len_check##_provider##___##_name[LTTNG_UST_SYM_NAME_LEN] \
135 __attribute__((unused)) = \
136 #_provider ":" #_name;
137
138 #include TRACEPOINT_INCLUDE
139
140 /*
141 * Stage 1 of tracepoint event generation.
142 *
143 * Create event field type metadata section.
144 * Each event produce an array of fields.
145 */
146
147 /* Reset all macros within TRACEPOINT_EVENT */
148 #include <lttng/ust-tracepoint-event-reset.h>
149
150 #undef ctf_integer_ext
151 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
152 { \
153 .name = #_item, \
154 .type = __type_integer(_type, _byte_order, _base, none),\
155 },
156
157 #undef ctf_float
158 #define ctf_float(_type, _item, _src) \
159 { \
160 .name = #_item, \
161 .type = __type_float(_type), \
162 },
163
164 #undef ctf_array_encoded
165 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
166 { \
167 .name = #_item, \
168 .type = \
169 { \
170 .atype = atype_array, \
171 .u.array = \
172 { \
173 .length = _length, \
174 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
175 }, \
176 }, \
177 },
178
179 #undef ctf_sequence_encoded
180 #define ctf_sequence_encoded(_type, _item, _src, \
181 _length_type, _src_length, _encoding) \
182 { \
183 .name = #_item, \
184 .type = \
185 { \
186 .atype = atype_sequence, \
187 .u.sequence = \
188 { \
189 .length_type = __type_integer(_length_type, BYTE_ORDER, 10, none), \
190 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
191 }, \
192 }, \
193 },
194
195 #undef ctf_string
196 #define ctf_string(_item, _src) \
197 { \
198 .name = #_item, \
199 .type = \
200 { \
201 .atype = atype_string, \
202 .u.basic.string.encoding = lttng_encode_UTF8, \
203 }, \
204 },
205
206 #undef TP_FIELDS
207 #define TP_FIELDS(...) __VA_ARGS__ /* Only one used in this phase */
208
209 #undef TRACEPOINT_EVENT_CLASS
210 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
211 static const struct lttng_event_field __event_fields___##_provider##___##_name[] = { \
212 _fields \
213 };
214
215 #include TRACEPOINT_INCLUDE
216
217 /*
218 * Stage 2 of tracepoint event generation.
219 *
220 * Create probe callback prototypes.
221 */
222
223 /* Reset all macros within TRACEPOINT_EVENT */
224 #include <lttng/ust-tracepoint-event-reset.h>
225
226 #undef TP_ARGS
227 #define TP_ARGS(...) __VA_ARGS__
228
229 #undef TRACEPOINT_EVENT_CLASS
230 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
231 static void __event_probe__##_provider##___##_name(_TP_ARGS_DATA_PROTO(_args));
232
233 #include TRACEPOINT_INCLUDE
234
235 /*
236 * Stage 3 of tracepoint event generation.
237 *
238 * Create static inline function that calculates event size.
239 */
240
241 /* Reset all macros within TRACEPOINT_EVENT */
242 #include <lttng/ust-tracepoint-event-reset.h>
243
244 #undef ctf_integer_ext
245 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
246 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
247 __event_len += sizeof(_type);
248
249 #undef ctf_float
250 #define ctf_float(_type, _item, _src) \
251 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
252 __event_len += sizeof(_type);
253
254 #undef ctf_array_encoded
255 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
256 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
257 __event_len += sizeof(_type) * (_length);
258
259 #undef ctf_sequence_encoded
260 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
261 _src_length, _encoding) \
262 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_length_type)); \
263 __event_len += sizeof(_length_type); \
264 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
265 __dynamic_len[__dynamic_len_idx] = (_src_length); \
266 __event_len += sizeof(_type) * __dynamic_len[__dynamic_len_idx]; \
267 __dynamic_len_idx++;
268
269 #undef ctf_string
270 #define ctf_string(_item, _src) \
271 __event_len += __dynamic_len[__dynamic_len_idx++] = strlen(_src) + 1;
272
273 #undef TP_ARGS
274 #define TP_ARGS(...) __VA_ARGS__
275
276 #undef TP_FIELDS
277 #define TP_FIELDS(...) __VA_ARGS__
278
279 #undef TRACEPOINT_EVENT_CLASS
280 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
281 static inline size_t __event_get_size__##_provider##___##_name(size_t *__dynamic_len, _TP_ARGS_DATA_PROTO(_args)) \
282 { \
283 size_t __event_len = 0; \
284 unsigned int __dynamic_len_idx = 0; \
285 \
286 if (0) \
287 (void) __dynamic_len_idx; /* don't warn if unused */ \
288 _fields \
289 return __event_len; \
290 }
291
292 #include TRACEPOINT_INCLUDE
293
294 /*
295 * Stage 4 of tracepoint event generation.
296 *
297 * Create static inline function that calculates event payload alignment.
298 */
299
300 /* Reset all macros within TRACEPOINT_EVENT */
301 #include <lttng/ust-tracepoint-event-reset.h>
302
303 #undef ctf_integer_ext
304 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
305 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
306
307 #undef ctf_float
308 #define ctf_float(_type, _item, _src) \
309 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
310
311 #undef ctf_array_encoded
312 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
313 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
314
315 #undef ctf_sequence_encoded
316 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
317 _src_length, _encoding) \
318 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_length_type)); \
319 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
320
321 #undef ctf_string
322 #define ctf_string(_item, _src)
323
324 #undef TP_ARGS
325 #define TP_ARGS(...) __VA_ARGS__
326
327 #undef TP_FIELDS
328 #define TP_FIELDS(...) __VA_ARGS__
329
330 #undef TRACEPOINT_EVENT_CLASS
331 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
332 static inline \
333 size_t __event_get_align__##_provider##___##_name(_TP_ARGS_PROTO(_args)) \
334 { \
335 size_t __event_align = 1; \
336 _fields \
337 return __event_align; \
338 }
339
340 #include TRACEPOINT_INCLUDE
341
342
343 /*
344 * Stage 5 of tracepoint event generation.
345 *
346 * Create the probe function. This function calls event size calculation
347 * and writes event data into the buffer.
348 */
349
350 /* Reset all macros within TRACEPOINT_EVENT */
351 #include <lttng/ust-tracepoint-event-reset.h>
352
353 #undef ctf_integer_ext
354 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
355 { \
356 _type __tmp = (_src); \
357 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
358 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
359 }
360
361 #undef ctf_float
362 #define ctf_float(_type, _item, _src) \
363 { \
364 _type __tmp = (_src); \
365 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
366 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
367 }
368
369 #undef ctf_array_encoded
370 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
371 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
372 __chan->ops->event_write(&__ctx, _src, sizeof(_type) * (_length));
373
374 #undef ctf_sequence_encoded
375 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
376 _src_length, _encoding) \
377 { \
378 _length_type __tmpl = __dynamic_len[__dynamic_len_idx]; \
379 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_length_type));\
380 __chan->ops->event_write(&__ctx, &__tmpl, sizeof(_length_type));\
381 } \
382 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
383 __chan->ops->event_write(&__ctx, _src, \
384 sizeof(_type) * __get_dynamic_len(dest));
385
386 #undef ctf_string
387 #define ctf_string(_item, _src) \
388 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(*(_src))); \
389 __chan->ops->event_write(&__ctx, _src, __get_dynamic_len(dest));
390
391 /* Beware: this get len actually consumes the len value */
392 #undef __get_dynamic_len
393 #define __get_dynamic_len(field) __dynamic_len[__dynamic_len_idx++]
394
395 #undef TP_ARGS
396 #define TP_ARGS(...) __VA_ARGS__
397
398 #undef TP_FIELDS
399 #define TP_FIELDS(...) __VA_ARGS__
400
401 #undef TRACEPOINT_EVENT_CLASS
402 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
403 static void __event_probe__##_provider##___##_name(_TP_ARGS_DATA_PROTO(_args))\
404 { \
405 struct ltt_event *__event = __tp_data; \
406 struct ltt_channel *__chan = __event->chan; \
407 struct lttng_ust_lib_ring_buffer_ctx __ctx; \
408 size_t __event_len, __event_align; \
409 size_t __dynamic_len_idx = 0; \
410 size_t __dynamic_len[_TP_ARRAY_SIZE(__event_fields___##_provider##___##_name)]; \
411 int __ret; \
412 \
413 if (0) \
414 (void) __dynamic_len_idx; /* don't warn if unused */ \
415 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->session->active))) \
416 return; \
417 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->enabled))) \
418 return; \
419 if (caa_unlikely(!CMM_ACCESS_ONCE(__event->enabled))) \
420 return; \
421 __event_len = __event_get_size__##_provider##___##_name(__dynamic_len,\
422 _TP_ARGS_DATA_VAR(_args)); \
423 __event_align = __event_get_align__##_provider##___##_name(_TP_ARGS_VAR(_args)); \
424 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
425 __event_align, -1, __chan->handle); \
426 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
427 if (__ret < 0) \
428 return; \
429 _fields \
430 __chan->ops->event_commit(&__ctx); \
431 }
432
433 #include TRACEPOINT_INCLUDE
434
435 #undef __get_dynamic_len
436
437 /*
438 * Stage 5.1 of tracepoint event generation.
439 *
440 * Create probe signature
441 */
442
443 /* Reset all macros within TRACEPOINT_EVENT */
444 #include <lttng/ust-tracepoint-event-reset.h>
445
446 #undef TP_ARGS
447 #define TP_ARGS(...) __VA_ARGS__
448
449 #define _TP_EXTRACT_STRING2(...) #__VA_ARGS__
450
451 #undef TRACEPOINT_EVENT_CLASS
452 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
453 const char __tp_event_signature___##_provider##___##_name[] = \
454 _TP_EXTRACT_STRING2(_args);
455
456 #include TRACEPOINT_INCLUDE
457
458 #undef _TP_EXTRACT_STRING2
459
460 /*
461 * Stage 6 of tracepoint event generation.
462 *
463 * Tracepoint loglevel mapping definition generation. We generate a
464 * symbol for each mapping for a provider/event to ensure at most a 1 to
465 * 1 mapping between events and loglevels. If the symbol is repeated,
466 * the compiler will complain.
467 */
468
469 /* Reset all macros within TRACEPOINT_EVENT */
470 #include <lttng/ust-tracepoint-event-reset.h>
471
472 #undef TRACEPOINT_LOGLEVEL
473 #define TRACEPOINT_LOGLEVEL(__provider, __name, __loglevel) \
474 static const int _loglevel_value___##__provider##___##__name = __loglevel; \
475 static const int *_loglevel___##__provider##___##__name = \
476 &_loglevel_value___##__provider##___##__name;
477
478 #include TRACEPOINT_INCLUDE
479
480 /*
481 * Stage 7.1 of tracepoint event generation.
482 *
483 * Create events description structures. We use a weakref because
484 * loglevels are optional. If not declared, the event will point to the
485 * a loglevel that contains NULL.
486 */
487
488 /* Reset all macros within TRACEPOINT_EVENT */
489 #include <lttng/ust-tracepoint-event-reset.h>
490
491 #undef TRACEPOINT_EVENT_INSTANCE
492 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
493 static const int * \
494 __ref_loglevel___##_provider##___##_name \
495 __attribute__((weakref ("_loglevel___" #_provider "___" #_name))); \
496 const struct lttng_event_desc __event_desc___##_provider##_##_name = { \
497 .fields = __event_fields___##_provider##___##_template, \
498 .name = #_provider ":" #_name, \
499 .probe_callback = (void (*)(void)) &__event_probe__##_provider##___##_template,\
500 .nr_fields = _TP_ARRAY_SIZE(__event_fields___##_provider##___##_template), \
501 .loglevel = &__ref_loglevel___##_provider##___##_name, \
502 .signature = __tp_event_signature___##_provider##___##_template, \
503 };
504
505 #include TRACEPOINT_INCLUDE
506
507 /*
508 * Stage 7.2 of tracepoint event generation.
509 *
510 * Create array of events.
511 */
512
513 /* Reset all macros within TRACEPOINT_EVENT */
514 #include <lttng/ust-tracepoint-event-reset.h>
515
516 #undef TRACEPOINT_EVENT_INSTANCE
517 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
518 &__event_desc___##_provider##_##_name,
519
520 static const struct lttng_event_desc *_TP_COMBINE_TOKENS(__event_desc___, TRACEPOINT_PROVIDER)[] = {
521 #include TRACEPOINT_INCLUDE
522 };
523
524
525 /*
526 * Stage 8 of tracepoint event generation.
527 *
528 * Create a toplevel descriptor for the whole probe.
529 */
530
531 /* non-const because list head will be modified when registered. */
532 static struct lttng_probe_desc _TP_COMBINE_TOKENS(__probe_desc___, TRACEPOINT_PROVIDER) = {
533 .provider = __tp_stringify(TRACEPOINT_PROVIDER),
534 .event_desc = _TP_COMBINE_TOKENS(__event_desc___, TRACEPOINT_PROVIDER),
535 .nr_events = _TP_ARRAY_SIZE(_TP_COMBINE_TOKENS(__event_desc___, TRACEPOINT_PROVIDER)),
536 };
537
538 /*
539 * Stage 9 of tracepoint event generation.
540 *
541 * Register/unregister probes at module load/unload.
542 *
543 * Generate the constructor as an externally visible symbol for use when
544 * linking the probe statically.
545 */
546
547 /* Reset all macros within TRACEPOINT_EVENT */
548 #include <lttng/ust-tracepoint-event-reset.h>
549 static void __attribute__((constructor))
550 _TP_COMBINE_TOKENS(__lttng_events_init__, TRACEPOINT_PROVIDER)(void)
551 {
552 int ret;
553
554 ret = ltt_probe_register(&_TP_COMBINE_TOKENS(__probe_desc___, TRACEPOINT_PROVIDER));
555 assert(!ret);
556 }
557
558 static void __attribute__((destructor))
559 _TP_COMBINE_TOKENS(__lttng_events_exit__, TRACEPOINT_PROVIDER)(void)
560 {
561 ltt_probe_unregister(&_TP_COMBINE_TOKENS(__probe_desc___, TRACEPOINT_PROVIDER));
562 }
563
564 int _TP_COMBINE_TOKENS(__tracepoint_provider_, TRACEPOINT_PROVIDER);
This page took 0.040731 seconds and 4 git commands to generate.