2 * Copyright (c) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
7 * Permission is hereby granted to use or copy this program
8 * for any purpose, provided the above notices are retained on all copies.
9 * Permission to modify the code and to distribute modified code is granted,
10 * provided the above notices are retained, and a notice that the code was
11 * modified is included with the above copyright notice.
15 #include <urcu/compiler.h>
16 #include <lttng/ust-events.h>
17 #include <lttng/ringbuffer-config.h>
21 * Macro declarations used for all stages.
25 #define ctf_integer(_type, _item, _src) \
26 ctf_integer_ext(_type, _item, _src, BYTE_ORDER, 10)
28 #undef ctf_integer_hex
29 #define ctf_integer_hex(_type, _item, _src) \
30 ctf_integer_ext(_type, _item, _src, BYTE_ORDER, 16)
32 #undef ctf_integer_network
33 #define ctf_integer_network(_type, _item, _src) \
34 ctf_integer_ext(_type, _item, _src, BIG_ENDIAN, 10)
36 #undef ctf_integer_network_hex
37 #define ctf_integer_network_hex(_type, _item, _src) \
38 ctf_integer_ext(_type, _item, _src, BIG_ENDIAN, 16)
40 /* ctf_float is redefined at each step */
43 #define ctf_array(_type, _item, _src, _length) \
44 ctf_array_encoded(_type, _item, _src, _length, none)
47 #define ctf_array_text(_type, _item, _src, _length) \
48 ctf_array_encoded(_type, _item, _src, _length, UTF8)
51 #define ctf_sequence(_type, _item, _src, _length_type, _src_length) \
52 ctf_sequence_encoded(_type, _item, _src, \
53 _length_type, _src_length, none)
55 #undef ctf_sequence_text
56 #define ctf_sequence_text(_type, _item, _src, _length_type, _src_length) \
57 ctf_sequence_encoded(_type, _item, _src, \
58 _length_type, _src_length, UTF8)
60 /* ctf_string is redefined at each step */
63 * TRACEPOINT_EVENT_CLASS declares a class of tracepoints receiving the
64 * same arguments and having the same field layout.
66 * TRACEPOINT_EVENT_INSTANCE declares an instance of a tracepoint, with
67 * its own provider and name. It refers to a class (template).
69 * TRACEPOINT_EVENT declared both a class and an instance and does a
70 * direct mapping from the instance to the class.
73 #undef TRACEPOINT_EVENT
74 #define TRACEPOINT_EVENT(_provider, _name, _args, _fields) \
75 TRACEPOINT_EVENT_CLASS(_provider, _name, \
77 _TP_PARAMS(_fields)) \
78 TRACEPOINT_EVENT_INSTANCE(_provider, _name, _name, \
82 #define _TP_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
84 #define _tp_max_t(type, x, y) \
88 __max1 > __max2 ? __max1: __max2; \
92 * Stage 0 of tracepoint event generation.
94 * Check that each TRACEPOINT_EVENT provider argument match the
95 * TRACEPOINT_PROVIDER by creating dummy callbacks.
98 /* Reset all macros within TRACEPOINT_EVENT */
99 #include <lttng/ust-tracepoint-event-reset.h>
102 void _TP_COMBINE_TOKENS(__tracepoint_provider_mismatch_
, TRACEPOINT_PROVIDER
)(void)
106 #undef TRACEPOINT_EVENT_CLASS
107 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
108 __tracepoint_provider_mismatch_##_provider();
110 #undef TRACEPOINT_EVENT_INSTANCE
111 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
112 __tracepoint_provider_mismatch_##_provider();
114 static __attribute__((unused
))
115 void _TP_COMBINE_TOKENS(__tracepoint_provider_check_
, TRACEPOINT_PROVIDER
)(void)
117 #include TRACEPOINT_INCLUDE
121 * Stage 1 of tracepoint event generation.
123 * Create event field type metadata section.
124 * Each event produce an array of fields.
127 /* Reset all macros within TRACEPOINT_EVENT */
128 #include <lttng/ust-tracepoint-event-reset.h>
130 #undef ctf_integer_ext
131 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
134 .type = __type_integer(_type, _byte_order, _base, none),\
138 #define ctf_float(_type, _item, _src) \
141 .type = __type_float(_type), \
144 #undef ctf_array_encoded
145 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
150 .atype = atype_array, \
154 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
159 #undef ctf_sequence_encoded
160 #define ctf_sequence_encoded(_type, _item, _src, \
161 _length_type, _src_length, _encoding) \
166 .atype = atype_sequence, \
169 .length_type = __type_integer(_length_type, BYTE_ORDER, 10, none), \
170 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
176 #define ctf_string(_item, _src) \
181 .atype = atype_string, \
182 .u.basic.string.encoding = lttng_encode_UTF8, \
187 #define TP_FIELDS(args...) args /* Only one used in this phase */
189 #undef TRACEPOINT_EVENT_CLASS
190 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
191 static const struct lttng_event_field __event_fields___##_provider##___##_name[] = { \
195 #include TRACEPOINT_INCLUDE
198 * Stage 2 of tracepoint event generation.
200 * Create probe callback prototypes.
203 /* Reset all macros within TRACEPOINT_EVENT */
204 #include <lttng/ust-tracepoint-event-reset.h>
207 #define TP_ARGS(args...) args
209 #undef TRACEPOINT_EVENT_CLASS
210 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
211 static void __event_probe__##_provider##___##_name(_TP_ARGS_DATA_PROTO(_args));
213 #include TRACEPOINT_INCLUDE
216 * Stage 3 of tracepoint event generation.
218 * Create an array of events.
221 /* Reset all macros within TRACEPOINT_EVENT */
222 #include <lttng/ust-tracepoint-event-reset.h>
224 #undef TRACEPOINT_EVENT_INSTANCE
225 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
227 .fields = __event_fields___##_provider##___##_template,\
228 .name = #_provider ":" #_name, \
229 .probe_callback = (void *) &__event_probe__##_provider##___##_template,\
230 .nr_fields = _TP_ARRAY_SIZE(__event_fields___##_provider##___##_template), \
233 static const struct lttng_event_desc
_TP_COMBINE_TOKENS(__event_desc___
, TRACEPOINT_PROVIDER
)[] = {
234 #include TRACEPOINT_INCLUDE
239 * Stage 4 of tracepoint event generation.
241 * Create static inline function that calculates event size.
244 /* Reset all macros within TRACEPOINT_EVENT */
245 #include <lttng/ust-tracepoint-event-reset.h>
247 #undef ctf_integer_ext
248 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
249 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
250 __event_len += sizeof(_type);
253 #define ctf_float(_type, _item, _src) \
254 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
255 __event_len += sizeof(_type);
257 #undef ctf_array_encoded
258 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
259 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
260 __event_len += sizeof(_type) * (_length);
262 #undef ctf_sequence_encoded
263 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
264 _src_length, _encoding) \
265 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_length_type)); \
266 __event_len += sizeof(_length_type); \
267 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
268 __dynamic_len[__dynamic_len_idx] = (_src_length); \
269 __event_len += sizeof(_type) * __dynamic_len[__dynamic_len_idx]; \
273 #define ctf_string(_item, _src) \
274 __event_len += __dynamic_len[__dynamic_len_idx++] = strlen(_src) + 1;
277 #define TP_ARGS(args...) args
280 #define TP_FIELDS(args...) args
282 #undef TRACEPOINT_EVENT_CLASS
283 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
284 static inline size_t __event_get_size__##_provider##___##_name(size_t *__dynamic_len, _TP_ARGS_DATA_PROTO(_args)) \
286 size_t __event_len = 0; \
287 unsigned int __dynamic_len_idx = 0; \
290 (void) __dynamic_len_idx; /* don't warn if unused */ \
292 return __event_len; \
295 #include TRACEPOINT_INCLUDE
298 * Stage 5 of tracepoint event generation.
300 * Create static inline function that calculates event payload alignment.
303 /* Reset all macros within TRACEPOINT_EVENT */
304 #include <lttng/ust-tracepoint-event-reset.h>
306 #undef ctf_integer_ext
307 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
308 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
311 #define ctf_float(_type, _item, _src) \
312 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
314 #undef ctf_array_encoded
315 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
316 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
318 #undef ctf_sequence_encoded
319 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
320 _src_length, _encoding) \
321 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_length_type)); \
322 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
325 #define ctf_string(_item, _src)
328 #define TP_ARGS(args...) args
331 #define TP_FIELDS(args...) args
333 #undef TRACEPOINT_EVENT_CLASS
334 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
336 size_t __event_get_align__##_provider##___##_name(_TP_ARGS_PROTO(_args)) \
338 size_t __event_align = 1; \
340 return __event_align; \
343 #include TRACEPOINT_INCLUDE
347 * Stage 6 of tracepoint event generation.
349 * Create the probe function. This function calls event size calculation
350 * and writes event data into the buffer.
353 /* Reset all macros within TRACEPOINT_EVENT */
354 #include <lttng/ust-tracepoint-event-reset.h>
356 #undef ctf_integer_ext
357 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
359 _type __tmp = (_src); \
360 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
361 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
365 #define ctf_float(_type, _item, _src) \
367 _type __tmp = (_src); \
368 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
369 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
372 #undef ctf_array_encoded
373 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
374 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
375 __chan->ops->event_write(&__ctx, _src, sizeof(_type) * (_length));
377 #undef ctf_sequence_encoded
378 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
379 _src_length, _encoding) \
381 _length_type __tmpl = __dynamic_len[__dynamic_len_idx]; \
382 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_length_type));\
383 __chan->ops->event_write(&__ctx, &__tmpl, sizeof(_length_type));\
385 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
386 __chan->ops->event_write(&__ctx, _src, \
387 sizeof(_type) * __get_dynamic_len(dest));
390 #define ctf_string(_item, _src) \
391 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(*(_src))); \
392 __chan->ops->event_write(&__ctx, _src, __get_dynamic_len(dest));
394 /* Beware: this get len actually consumes the len value */
395 #undef __get_dynamic_len
396 #define __get_dynamic_len(field) __dynamic_len[__dynamic_len_idx++]
399 #define TP_ARGS(args...) args
402 #define TP_FIELDS(args...) args
404 #undef TRACEPOINT_EVENT_CLASS
405 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
406 static void __event_probe__##_provider##___##_name(_TP_ARGS_DATA_PROTO(_args))\
408 struct ltt_event *__event = __tp_data; \
409 struct ltt_channel *__chan = __event->chan; \
410 struct lttng_ust_lib_ring_buffer_ctx __ctx; \
411 size_t __event_len, __event_align; \
412 size_t __dynamic_len_idx = 0; \
413 size_t __dynamic_len[_TP_ARRAY_SIZE(__event_fields___##_provider##___##_name)]; \
417 (void) __dynamic_len_idx; /* don't warn if unused */ \
418 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->session->active))) \
420 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->enabled))) \
422 if (caa_unlikely(!CMM_ACCESS_ONCE(__event->enabled))) \
424 __event_len = __event_get_size__##_provider##___##_name(__dynamic_len,\
425 _TP_ARGS_DATA_VAR(_args)); \
426 __event_align = __event_get_align__##_provider##___##_name(_TP_ARGS_VAR(_args)); \
427 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
428 __event_align, -1, __chan->handle); \
429 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
433 __chan->ops->event_commit(&__ctx); \
436 #include TRACEPOINT_INCLUDE
438 #undef __get_dynamic_len
441 * Stage 7.1 of tracepoint event generation.
443 * Tracepoint loglevel enumeration definition generation.
446 /* Reset all macros within TRACEPOINT_EVENT */
447 #include <lttng/ust-tracepoint-event-reset.h>
449 #undef TRACEPOINT_LOGLEVEL_ENUM
450 #define TRACEPOINT_LOGLEVEL_ENUM(...) __VA_ARGS__
453 #define TP_LOGLEVEL(_identifier, _value) \
454 static const struct tracepoint_loglevel_enum_entry \
455 _TP_COMBINE_TOKENS(_TP_COMBINE_TOKENS(TRACEPOINT_PROVIDER, __tp_loglevel_enum_entry__), _identifier) = \
457 .identifier = #_identifier, \
461 #include TRACEPOINT_INCLUDE
464 * Stage 7.2 of tracepoint event generation.
466 * Tracepoint loglevel enumeration array generation.
469 /* Reset all macros within TRACEPOINT_EVENT */
470 #include <lttng/ust-tracepoint-event-reset.h>
472 #undef TRACEPOINT_LOGLEVEL_ENUM
473 #define TRACEPOINT_LOGLEVEL_ENUM(...) __VA_ARGS__
476 #define TP_LOGLEVEL(_identifier, _value) \
477 &_TP_COMBINE_TOKENS(_TP_COMBINE_TOKENS(TRACEPOINT_PROVIDER, __tp_loglevel_enum_entry__), _identifier),
479 static const struct tracepoint_loglevel_enum_entry
*_TP_COMBINE_TOKENS(__tracepoint_loglevel_enum__
, TRACEPOINT_PROVIDER
)[] __attribute__((unused
)) =
481 #include TRACEPOINT_INCLUDE
485 * Stage 8 of tracepoint event generation.
487 * Tracepoint loglevel definition generation.
490 /* Reset all macros within TRACEPOINT_EVENT */
491 #include <lttng/ust-tracepoint-event-reset.h>
493 #undef TRACEPOINT_LOGLEVEL
494 #define TRACEPOINT_LOGLEVEL(__provider, __name, __loglevel) \
496 .name = #__provider ":" #__name, \
497 .loglevel = &_TP_COMBINE_TOKENS(_TP_COMBINE_TOKENS(TRACEPOINT_PROVIDER, __tp_loglevel_enum_entry__), __loglevel), \
500 static struct tracepoint_loglevel
_TP_COMBINE_TOKENS(__tracepoint_loglevels__
, TRACEPOINT_PROVIDER
)[] = {
501 #include TRACEPOINT_INCLUDE
505 * Stage 9 of tracepoint event generation.
507 * Create a toplevel descriptor for the whole probe.
510 /* non-const because list head will be modified when registered. */
511 static struct lttng_probe_desc
_TP_COMBINE_TOKENS(__probe_desc___
, TRACEPOINT_PROVIDER
) = {
512 .event_desc
= _TP_COMBINE_TOKENS(__event_desc___
, TRACEPOINT_PROVIDER
),
513 .nr_events
= _TP_ARRAY_SIZE(_TP_COMBINE_TOKENS(__event_desc___
, TRACEPOINT_PROVIDER
)),
514 .loglevels
= _TP_COMBINE_TOKENS(__tracepoint_loglevels__
, TRACEPOINT_PROVIDER
),
518 * Stage 10 of tracepoint event generation.
520 * Register/unregister probes at module load/unload.
523 /* Reset all macros within TRACEPOINT_EVENT */
524 #include <lttng/ust-tracepoint-event-reset.h>
526 static void __attribute__((constructor
))
527 _TP_COMBINE_TOKENS(__lttng_events_init__
, TRACEPOINT_PROVIDER
)(void)
531 ret
= ltt_probe_register(&_TP_COMBINE_TOKENS(__probe_desc___
, TRACEPOINT_PROVIDER
));
535 static void __attribute__((destructor
))
536 _TP_COMBINE_TOKENS(__lttng_events_exit__
, TRACEPOINT_PROVIDER
)(void)
538 ltt_probe_unregister(&_TP_COMBINE_TOKENS(__probe_desc___
, TRACEPOINT_PROVIDER
));