2 * Copyright (c) 2011-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
16 #include <urcu/compiler.h>
17 #include <lttng/ust-events.h>
18 #include <lttng/ringbuffer-config.h>
22 * Macro declarations used for all stages.
26 #define ctf_integer(_type, _item, _src) \
27 ctf_integer_ext(_type, _item, _src, BYTE_ORDER, 10)
29 #undef ctf_integer_hex
30 #define ctf_integer_hex(_type, _item, _src) \
31 ctf_integer_ext(_type, _item, _src, BYTE_ORDER, 16)
33 #undef ctf_integer_network
34 #define ctf_integer_network(_type, _item, _src) \
35 ctf_integer_ext(_type, _item, _src, BIG_ENDIAN, 10)
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)
41 /* ctf_float is redefined at each step */
44 #define ctf_array(_type, _item, _src, _length) \
45 ctf_array_encoded(_type, _item, _src, _length, none)
48 #define ctf_array_text(_type, _item, _src, _length) \
49 ctf_array_encoded(_type, _item, _src, _length, UTF8)
52 #define ctf_sequence(_type, _item, _src, _length_type, _src_length) \
53 ctf_sequence_encoded(_type, _item, _src, \
54 _length_type, _src_length, none)
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)
61 /* ctf_string is redefined at each step */
64 * TRACEPOINT_EVENT_CLASS declares a class of tracepoints receiving the
65 * same arguments and having the same field layout.
67 * TRACEPOINT_EVENT_INSTANCE declares an instance of a tracepoint, with
68 * its own provider and name. It refers to a class (template).
70 * TRACEPOINT_EVENT declared both a class and an instance and does a
71 * direct mapping from the instance to the class.
74 #undef TRACEPOINT_EVENT
75 #define TRACEPOINT_EVENT(_provider, _name, _args, _fields) \
76 TRACEPOINT_EVENT_CLASS(_provider, _name, \
78 _TP_PARAMS(_fields)) \
79 TRACEPOINT_EVENT_INSTANCE(_provider, _name, _name, \
83 #define _TP_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
85 #define _tp_max_t(type, x, y) \
89 __max1 > __max2 ? __max1: __max2; \
93 * Stage 0 of tracepoint event generation.
95 * Check that each TRACEPOINT_EVENT provider argument match the
96 * TRACEPOINT_PROVIDER by creating dummy callbacks.
99 /* Reset all macros within TRACEPOINT_EVENT */
100 #include <lttng/ust-tracepoint-event-reset.h>
103 void _TP_COMBINE_TOKENS(__tracepoint_provider_mismatch_
, TRACEPOINT_PROVIDER
)(void)
107 #undef TRACEPOINT_EVENT_CLASS
108 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
109 __tracepoint_provider_mismatch_##_provider();
111 #undef TRACEPOINT_EVENT_INSTANCE
112 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
113 __tracepoint_provider_mismatch_##_provider();
115 static __attribute__((unused
))
116 void _TP_COMBINE_TOKENS(__tracepoint_provider_check_
, TRACEPOINT_PROVIDER
)(void)
118 #include TRACEPOINT_INCLUDE
122 * Stage 0.1 of tracepoint event generation.
124 * Check that each TRACEPOINT_EVENT provider:name does not exceed the
125 * tracepoint name length limit.
128 /* Reset all macros within TRACEPOINT_EVENT */
129 #include <lttng/ust-tracepoint-event-reset.h>
131 #undef TRACEPOINT_EVENT_INSTANCE
132 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
134 __tp_name_len_check##_provider##___##_name[LTTNG_UST_SYM_NAME_LEN] \
135 __attribute__((unused)) = \
136 #_provider ":" #_name;
138 #include TRACEPOINT_INCLUDE
141 * Stage 1 of tracepoint event generation.
143 * Create event field type metadata section.
144 * Each event produce an array of fields.
147 /* Reset all macros within TRACEPOINT_EVENT */
148 #include <lttng/ust-tracepoint-event-reset.h>
150 #undef ctf_integer_ext
151 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
154 .type = __type_integer(_type, _byte_order, _base, none),\
158 #define ctf_float(_type, _item, _src) \
161 .type = __type_float(_type), \
164 #undef ctf_array_encoded
165 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
170 .atype = atype_array, \
174 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
179 #undef ctf_sequence_encoded
180 #define ctf_sequence_encoded(_type, _item, _src, \
181 _length_type, _src_length, _encoding) \
186 .atype = atype_sequence, \
189 .length_type = __type_integer(_length_type, BYTE_ORDER, 10, none), \
190 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
196 #define ctf_string(_item, _src) \
201 .atype = atype_string, \
202 .u.basic.string.encoding = lttng_encode_UTF8, \
207 #define TP_FIELDS(args...) args /* Only one used in this phase */
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[] = { \
215 #include TRACEPOINT_INCLUDE
218 * Stage 2 of tracepoint event generation.
220 * Create probe callback prototypes.
223 /* Reset all macros within TRACEPOINT_EVENT */
224 #include <lttng/ust-tracepoint-event-reset.h>
227 #define TP_ARGS(args...) args
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));
233 #include TRACEPOINT_INCLUDE
236 * Stage 3 of tracepoint event generation.
238 * Create static inline function that calculates event size.
241 /* Reset all macros within TRACEPOINT_EVENT */
242 #include <lttng/ust-tracepoint-event-reset.h>
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);
250 #define ctf_float(_type, _item, _src) \
251 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
252 __event_len += sizeof(_type);
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);
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]; \
270 #define ctf_string(_item, _src) \
271 __event_len += __dynamic_len[__dynamic_len_idx++] = strlen(_src) + 1;
274 #define TP_ARGS(args...) args
277 #define TP_FIELDS(args...) args
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)) \
283 size_t __event_len = 0; \
284 unsigned int __dynamic_len_idx = 0; \
287 (void) __dynamic_len_idx; /* don't warn if unused */ \
289 return __event_len; \
292 #include TRACEPOINT_INCLUDE
295 * Stage 4 of tracepoint event generation.
297 * Create static inline function that calculates event payload alignment.
300 /* Reset all macros within TRACEPOINT_EVENT */
301 #include <lttng/ust-tracepoint-event-reset.h>
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));
308 #define ctf_float(_type, _item, _src) \
309 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
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));
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));
322 #define ctf_string(_item, _src)
325 #define TP_ARGS(args...) args
328 #define TP_FIELDS(args...) args
330 #undef TRACEPOINT_EVENT_CLASS
331 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
333 size_t __event_get_align__##_provider##___##_name(_TP_ARGS_PROTO(_args)) \
335 size_t __event_align = 1; \
337 return __event_align; \
340 #include TRACEPOINT_INCLUDE
344 * Stage 5 of tracepoint event generation.
346 * Create the probe function. This function calls event size calculation
347 * and writes event data into the buffer.
350 /* Reset all macros within TRACEPOINT_EVENT */
351 #include <lttng/ust-tracepoint-event-reset.h>
353 #undef ctf_integer_ext
354 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
356 _type __tmp = (_src); \
357 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
358 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
362 #define ctf_float(_type, _item, _src) \
364 _type __tmp = (_src); \
365 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
366 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
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));
374 #undef ctf_sequence_encoded
375 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
376 _src_length, _encoding) \
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));\
382 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
383 __chan->ops->event_write(&__ctx, _src, \
384 sizeof(_type) * __get_dynamic_len(dest));
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));
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++]
396 #define TP_ARGS(args...) args
399 #define TP_FIELDS(args...) args
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))\
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)]; \
414 (void) __dynamic_len_idx; /* don't warn if unused */ \
415 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->session->active))) \
417 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->enabled))) \
419 if (caa_unlikely(!CMM_ACCESS_ONCE(__event->enabled))) \
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); \
430 __chan->ops->event_commit(&__ctx); \
433 #include TRACEPOINT_INCLUDE
435 #undef __get_dynamic_len
438 * Stage 5.1 of tracepoint event generation.
440 * Create probe signature
443 /* Reset all macros within TRACEPOINT_EVENT */
444 #include <lttng/ust-tracepoint-event-reset.h>
447 #define TP_ARGS(args...) args
449 #define _TP_EXTRACT_STRING2(...) #__VA_ARGS__
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);
456 #include TRACEPOINT_INCLUDE
458 #undef _TP_EXTRACT_STRING2
461 * Stage 6 of tracepoint event generation.
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.
469 /* Reset all macros within TRACEPOINT_EVENT */
470 #include <lttng/ust-tracepoint-event-reset.h>
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;
478 #include TRACEPOINT_INCLUDE
481 * Stage 7.1 of tracepoint event generation.
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.
488 /* Reset all macros within TRACEPOINT_EVENT */
489 #include <lttng/ust-tracepoint-event-reset.h>
491 #undef TRACEPOINT_EVENT_INSTANCE
492 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
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 *) &__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, \
505 #include TRACEPOINT_INCLUDE
508 * Stage 7.2 of tracepoint event generation.
510 * Create array of events.
513 /* Reset all macros within TRACEPOINT_EVENT */
514 #include <lttng/ust-tracepoint-event-reset.h>
516 #undef TRACEPOINT_EVENT_INSTANCE
517 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
518 &__event_desc___##_provider##_##_name,
520 static const struct lttng_event_desc
*_TP_COMBINE_TOKENS(__event_desc___
, TRACEPOINT_PROVIDER
)[] = {
521 #include TRACEPOINT_INCLUDE
526 * Stage 8 of tracepoint event generation.
528 * Create a toplevel descriptor for the whole probe.
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
)),
539 * Stage 9 of tracepoint event generation.
541 * Register/unregister probes at module load/unload.
543 * Generate the constructor as an externally visible symbol for use when
544 * linking the probe statically.
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)
554 ret
= ltt_probe_register(&_TP_COMBINE_TOKENS(__probe_desc___
, TRACEPOINT_PROVIDER
));
558 static void __attribute__((destructor
))
559 _TP_COMBINE_TOKENS(__lttng_events_exit__
, TRACEPOINT_PROVIDER
)(void)
561 ltt_probe_unregister(&_TP_COMBINE_TOKENS(__probe_desc___
, TRACEPOINT_PROVIDER
));
564 int _TP_COMBINE_TOKENS(__tracepoint_provider_
, TRACEPOINT_PROVIDER
);