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 static inline function that calculates event size.
221 /* Reset all macros within TRACEPOINT_EVENT */
222 #include <lttng/ust-tracepoint-event-reset.h>
224 #undef ctf_integer_ext
225 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
226 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
227 __event_len += sizeof(_type);
230 #define ctf_float(_type, _item, _src) \
231 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
232 __event_len += sizeof(_type);
234 #undef ctf_array_encoded
235 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
236 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
237 __event_len += sizeof(_type) * (_length);
239 #undef ctf_sequence_encoded
240 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
241 _src_length, _encoding) \
242 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_length_type)); \
243 __event_len += sizeof(_length_type); \
244 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
245 __dynamic_len[__dynamic_len_idx] = (_src_length); \
246 __event_len += sizeof(_type) * __dynamic_len[__dynamic_len_idx]; \
250 #define ctf_string(_item, _src) \
251 __event_len += __dynamic_len[__dynamic_len_idx++] = strlen(_src) + 1;
254 #define TP_ARGS(args...) args
257 #define TP_FIELDS(args...) args
259 #undef TRACEPOINT_EVENT_CLASS
260 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
261 static inline size_t __event_get_size__##_provider##___##_name(size_t *__dynamic_len, _TP_ARGS_DATA_PROTO(_args)) \
263 size_t __event_len = 0; \
264 unsigned int __dynamic_len_idx = 0; \
267 (void) __dynamic_len_idx; /* don't warn if unused */ \
269 return __event_len; \
272 #include TRACEPOINT_INCLUDE
275 * Stage 4 of tracepoint event generation.
277 * Create static inline function that calculates event payload alignment.
280 /* Reset all macros within TRACEPOINT_EVENT */
281 #include <lttng/ust-tracepoint-event-reset.h>
283 #undef ctf_integer_ext
284 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
285 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
288 #define ctf_float(_type, _item, _src) \
289 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
291 #undef ctf_array_encoded
292 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
293 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
295 #undef ctf_sequence_encoded
296 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
297 _src_length, _encoding) \
298 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_length_type)); \
299 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
302 #define ctf_string(_item, _src)
305 #define TP_ARGS(args...) args
308 #define TP_FIELDS(args...) args
310 #undef TRACEPOINT_EVENT_CLASS
311 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
313 size_t __event_get_align__##_provider##___##_name(_TP_ARGS_PROTO(_args)) \
315 size_t __event_align = 1; \
317 return __event_align; \
320 #include TRACEPOINT_INCLUDE
324 * Stage 5 of tracepoint event generation.
326 * Create the probe function. This function calls event size calculation
327 * and writes event data into the buffer.
330 /* Reset all macros within TRACEPOINT_EVENT */
331 #include <lttng/ust-tracepoint-event-reset.h>
333 #undef ctf_integer_ext
334 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
336 _type __tmp = (_src); \
337 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
338 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
342 #define ctf_float(_type, _item, _src) \
344 _type __tmp = (_src); \
345 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
346 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
349 #undef ctf_array_encoded
350 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
351 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
352 __chan->ops->event_write(&__ctx, _src, sizeof(_type) * (_length));
354 #undef ctf_sequence_encoded
355 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
356 _src_length, _encoding) \
358 _length_type __tmpl = __dynamic_len[__dynamic_len_idx]; \
359 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_length_type));\
360 __chan->ops->event_write(&__ctx, &__tmpl, sizeof(_length_type));\
362 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
363 __chan->ops->event_write(&__ctx, _src, \
364 sizeof(_type) * __get_dynamic_len(dest));
367 #define ctf_string(_item, _src) \
368 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(*(_src))); \
369 __chan->ops->event_write(&__ctx, _src, __get_dynamic_len(dest));
371 /* Beware: this get len actually consumes the len value */
372 #undef __get_dynamic_len
373 #define __get_dynamic_len(field) __dynamic_len[__dynamic_len_idx++]
376 #define TP_ARGS(args...) args
379 #define TP_FIELDS(args...) args
381 #undef TRACEPOINT_EVENT_CLASS
382 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
383 static void __event_probe__##_provider##___##_name(_TP_ARGS_DATA_PROTO(_args))\
385 struct ltt_event *__event = __tp_data; \
386 struct ltt_channel *__chan = __event->chan; \
387 struct lttng_ust_lib_ring_buffer_ctx __ctx; \
388 size_t __event_len, __event_align; \
389 size_t __dynamic_len_idx = 0; \
390 size_t __dynamic_len[_TP_ARRAY_SIZE(__event_fields___##_provider##___##_name)]; \
394 (void) __dynamic_len_idx; /* don't warn if unused */ \
395 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->session->active))) \
397 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->enabled))) \
399 if (caa_unlikely(!CMM_ACCESS_ONCE(__event->enabled))) \
401 __event_len = __event_get_size__##_provider##___##_name(__dynamic_len,\
402 _TP_ARGS_DATA_VAR(_args)); \
403 __event_align = __event_get_align__##_provider##___##_name(_TP_ARGS_VAR(_args)); \
404 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
405 __event_align, -1, __chan->handle); \
406 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
410 __chan->ops->event_commit(&__ctx); \
413 #include TRACEPOINT_INCLUDE
415 #undef __get_dynamic_len
418 * Stage 6.1 of tracepoint event generation.
420 * Tracepoint loglevel enumeration definition generation.
423 /* Reset all macros within TRACEPOINT_EVENT */
424 #include <lttng/ust-tracepoint-event-reset.h>
426 #undef TRACEPOINT_LOGLEVEL_ENUM
427 #define TRACEPOINT_LOGLEVEL_ENUM(...) __VA_ARGS__
430 #define tp_loglevel(_identifier, _value) \
431 static const struct tracepoint_loglevel_entry \
432 _TP_COMBINE_TOKENS4(__tp_loglevel_entry__, TRACEPOINT_PROVIDER, ___, _identifier) = { \
433 .identifier = #_identifier, \
437 #include TRACEPOINT_INCLUDE
440 * Stage 6.2 of tracepoint event generation.
442 * Tracepoint loglevel enumeration array generation.
445 /* Reset all macros within TRACEPOINT_EVENT */
446 #include <lttng/ust-tracepoint-event-reset.h>
448 #undef TRACEPOINT_LOGLEVEL_ENUM
449 #define TRACEPOINT_LOGLEVEL_ENUM(...) __VA_ARGS__
452 #define tp_loglevel(_identifier, _value) \
453 &_TP_COMBINE_TOKENS4(__tp_loglevel_entry__, TRACEPOINT_PROVIDER, ___, _identifier), \
455 static const struct tracepoint_loglevel_entry *_TP_COMBINE_TOKENS(__tracepoint_loglevel_enum__, TRACEPOINT_PROVIDER)[] = {
456 #include TRACEPOINT_INCLUDE
460 * Stage 7 of tracepoint event generation.
462 * Tracepoint loglevel mapping definition generation. We generate a
463 * symbol for each mapping for a provider/event to ensure at most a 1 to
464 * 1 mapping between events and loglevels. If the symbol is repeated,
465 * the compiler will complain.
468 /* Reset all macros within TRACEPOINT_EVENT */
469 #include <lttng/ust-tracepoint-event-reset.h>
471 #undef TRACEPOINT_LOGLEVEL
472 #define TRACEPOINT_LOGLEVEL(__provider, __name, __loglevel) \
473 static const struct tracepoint_loglevel_entry * \
474 _loglevel_mapping___##__provider##___##__name = \
475 &__tp_loglevel_entry__##__provider##___##__loglevel;
477 #include TRACEPOINT_INCLUDE
480 * Stage 8.1 of tracepoint event generation.
482 * Create events description structures. We use a weakref because
483 * loglevels are optional. If not declared, the event will point to the
484 * a loglevel that contains NULL.
487 /* Reset all macros within TRACEPOINT_EVENT */
488 #include <lttng/ust-tracepoint-event-reset.h>
490 #undef TRACEPOINT_EVENT_INSTANCE
491 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
492 static const struct tracepoint_loglevel_entry * \
493 __ref_loglevel_mapping___##_provider##___##_name \
494 __attribute__((weakref ("_loglevel_mapping___" #_provider "___" #_name))); \
495 const struct lttng_event_desc __event_desc___##_provider##_##_name = { \
496 .fields = __event_fields___##_provider##___##_template, \
497 .name = #_provider ":" #_name, \
498 .probe_callback = (void *) &__event_probe__##_provider##___##_template,\
499 .nr_fields = _TP_ARRAY_SIZE(__event_fields___##_provider##___##_template), \
500 .loglevel = &__ref_loglevel_mapping___##_provider##___##_name, \
503 #include TRACEPOINT_INCLUDE
506 * Stage 8.2 of tracepoint event generation.
508 * Create array of events.
511 /* Reset all macros within TRACEPOINT_EVENT */
512 #include <lttng/ust-tracepoint-event-reset.h>
514 #undef TRACEPOINT_EVENT_INSTANCE
515 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
516 &__event_desc___##_provider##_##_name,
518 static const struct lttng_event_desc
*_TP_COMBINE_TOKENS(__event_desc___
, TRACEPOINT_PROVIDER
)[] = {
519 #include TRACEPOINT_INCLUDE
524 * Stage 9 of tracepoint event generation.
526 * Create a toplevel descriptor for the whole probe.
529 /* non-const because list head will be modified when registered. */
530 static struct lttng_probe_desc
_TP_COMBINE_TOKENS(__probe_desc___
, TRACEPOINT_PROVIDER
) = {
531 .provider
= __tp_stringify(TRACEPOINT_PROVIDER
),
532 .event_desc
= _TP_COMBINE_TOKENS(__event_desc___
, TRACEPOINT_PROVIDER
),
533 .nr_events
= _TP_ARRAY_SIZE(_TP_COMBINE_TOKENS(__event_desc___
, TRACEPOINT_PROVIDER
)),
534 .loglevels
= _TP_COMBINE_TOKENS(__tracepoint_loglevel_enum__
, TRACEPOINT_PROVIDER
),
535 .nr_loglevels
= _TP_ARRAY_SIZE(_TP_COMBINE_TOKENS(__tracepoint_loglevel_enum__
, TRACEPOINT_PROVIDER
)),
539 * Stage 10 of tracepoint event generation.
541 * Register/unregister probes at module load/unload.
544 /* Reset all macros within TRACEPOINT_EVENT */
545 #include <lttng/ust-tracepoint-event-reset.h>
547 static void __attribute__((constructor
))
548 _TP_COMBINE_TOKENS(__lttng_events_init__
, TRACEPOINT_PROVIDER
)(void)
552 ret
= ltt_probe_register(&_TP_COMBINE_TOKENS(__probe_desc___
, TRACEPOINT_PROVIDER
));
556 static void __attribute__((destructor
))
557 _TP_COMBINE_TOKENS(__lttng_events_exit__
, TRACEPOINT_PROVIDER
)(void)
559 ltt_probe_unregister(&_TP_COMBINE_TOKENS(__probe_desc___
, TRACEPOINT_PROVIDER
));