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.
17 #include <urcu/compiler.h>
18 #include <lttng/ust-events.h>
19 #include <lttng/ringbuffer-config.h>
23 * Macro declarations used for all stages.
27 #define ctf_integer(_type, _item, _src) \
28 ctf_integer_ext(_type, _item, _src, BYTE_ORDER, 10)
30 #undef ctf_integer_hex
31 #define ctf_integer_hex(_type, _item, _src) \
32 ctf_integer_ext(_type, _item, _src, BYTE_ORDER, 16)
34 #undef ctf_integer_network
35 #define ctf_integer_network(_type, _item, _src) \
36 ctf_integer_ext(_type, _item, _src, BIG_ENDIAN, 10)
38 #undef ctf_integer_network_hex
39 #define ctf_integer_network_hex(_type, _item, _src) \
40 ctf_integer_ext(_type, _item, _src, BIG_ENDIAN, 16)
42 /* ctf_float is redefined at each step */
45 #define ctf_array(_type, _item, _src, _length) \
46 ctf_array_encoded(_type, _item, _src, _length, none)
49 #define ctf_array_text(_type, _item, _src, _length) \
50 ctf_array_encoded(_type, _item, _src, _length, UTF8)
53 #define ctf_sequence(_type, _item, _src, _length_type, _src_length) \
54 ctf_sequence_encoded(_type, _item, _src, \
55 _length_type, _src_length, none)
57 #undef ctf_sequence_text
58 #define ctf_sequence_text(_type, _item, _src, _length_type, _src_length) \
59 ctf_sequence_encoded(_type, _item, _src, \
60 _length_type, _src_length, UTF8)
62 /* ctf_string is redefined at each step */
65 * TRACEPOINT_EVENT_CLASS declares a class of tracepoints receiving the
66 * same arguments and having the same field layout.
68 * TRACEPOINT_EVENT_INSTANCE declares an instance of a tracepoint, with
69 * its own provider and name. It refers to a class (template).
71 * TRACEPOINT_EVENT declared both a class and an instance and does a
72 * direct mapping from the instance to the class.
75 #undef TRACEPOINT_EVENT
76 #define TRACEPOINT_EVENT(_provider, _name, _args, _fields) \
77 TRACEPOINT_EVENT_CLASS(_provider, _name, \
79 _TP_PARAMS(_fields)) \
80 TRACEPOINT_EVENT_INSTANCE(_provider, _name, _name, \
84 #define _TP_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
86 #define _tp_max_t(type, x, y) \
90 __max1 > __max2 ? __max1: __max2; \
94 * Stage 0 of tracepoint event generation.
96 * Check that each TRACEPOINT_EVENT provider argument match the
97 * TRACEPOINT_PROVIDER by creating dummy callbacks.
100 /* Reset all macros within TRACEPOINT_EVENT */
101 #include <lttng/ust-tracepoint-event-reset.h>
104 void _TP_COMBINE_TOKENS(__tracepoint_provider_mismatch_
, TRACEPOINT_PROVIDER
)(void)
108 #undef TRACEPOINT_EVENT_CLASS
109 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
110 __tracepoint_provider_mismatch_##_provider();
112 #undef TRACEPOINT_EVENT_INSTANCE
113 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
114 __tracepoint_provider_mismatch_##_provider();
117 void _TP_COMBINE_TOKENS(__tracepoint_provider_check_
, TRACEPOINT_PROVIDER
)(void)
119 #include TRACEPOINT_INCLUDE
123 * Stage 0.1 of tracepoint event generation.
125 * Check that each TRACEPOINT_EVENT provider:name does not exceed the
126 * tracepoint name length limit.
129 /* Reset all macros within TRACEPOINT_EVENT */
130 #include <lttng/ust-tracepoint-event-reset.h>
132 #undef TRACEPOINT_EVENT_INSTANCE
133 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
135 __tp_name_len_check##_provider##___##_name[LTTNG_UST_SYM_NAME_LEN] \
136 __attribute__((unused)) = \
137 #_provider ":" #_name;
139 #include TRACEPOINT_INCLUDE
142 * Stage 1 of tracepoint event generation.
144 * Create event field type metadata section.
145 * Each event produce an array of fields.
148 /* Reset all macros within TRACEPOINT_EVENT */
149 #include <lttng/ust-tracepoint-event-reset.h>
151 #undef ctf_integer_ext
152 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
155 .type = __type_integer(_type, _byte_order, _base, none),\
159 #define ctf_float(_type, _item, _src) \
162 .type = __type_float(_type), \
165 #undef ctf_array_encoded
166 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
171 .atype = atype_array, \
175 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
180 #undef ctf_sequence_encoded
181 #define ctf_sequence_encoded(_type, _item, _src, \
182 _length_type, _src_length, _encoding) \
187 .atype = atype_sequence, \
190 .length_type = __type_integer(_length_type, BYTE_ORDER, 10, none), \
191 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
197 #define ctf_string(_item, _src) \
202 .atype = atype_string, \
203 .u.basic.string.encoding = lttng_encode_UTF8, \
208 #define TP_FIELDS(...) __VA_ARGS__ /* Only one used in this phase */
210 #undef TRACEPOINT_EVENT_CLASS
211 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
212 static const struct lttng_event_field __event_fields___##_provider##___##_name[] = { \
216 #include TRACEPOINT_INCLUDE
219 * Stage 2 of tracepoint event generation.
221 * Create probe callback prototypes.
224 /* Reset all macros within TRACEPOINT_EVENT */
225 #include <lttng/ust-tracepoint-event-reset.h>
228 #define TP_ARGS(...) __VA_ARGS__
230 #undef TRACEPOINT_EVENT_CLASS
231 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
232 static void __event_probe__##_provider##___##_name(_TP_ARGS_DATA_PROTO(_args));
234 #include TRACEPOINT_INCLUDE
237 * Stage 3 of tracepoint event generation.
239 * Create static inline function that calculates event size.
242 /* Reset all macros within TRACEPOINT_EVENT */
243 #include <lttng/ust-tracepoint-event-reset.h>
245 #undef ctf_integer_ext
246 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
247 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
248 __event_len += sizeof(_type);
251 #define ctf_float(_type, _item, _src) \
252 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
253 __event_len += sizeof(_type);
255 #undef ctf_array_encoded
256 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
257 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
258 __event_len += sizeof(_type) * (_length);
260 #undef ctf_sequence_encoded
261 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
262 _src_length, _encoding) \
263 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_length_type)); \
264 __event_len += sizeof(_length_type); \
265 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
266 __dynamic_len[__dynamic_len_idx] = (_src_length); \
267 __event_len += sizeof(_type) * __dynamic_len[__dynamic_len_idx]; \
271 #define ctf_string(_item, _src) \
272 __event_len += __dynamic_len[__dynamic_len_idx++] = strlen(_src) + 1;
275 #define TP_ARGS(...) __VA_ARGS__
278 #define TP_FIELDS(...) __VA_ARGS__
280 #undef TRACEPOINT_EVENT_CLASS
281 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
282 static inline size_t __event_get_size__##_provider##___##_name(size_t *__dynamic_len, _TP_ARGS_DATA_PROTO(_args)) \
284 size_t __event_len = 0; \
285 unsigned int __dynamic_len_idx = 0; \
288 (void) __dynamic_len_idx; /* don't warn if unused */ \
290 return __event_len; \
293 #include TRACEPOINT_INCLUDE
296 * Stage 4 of tracepoint event generation.
298 * Create static inline function that calculates event payload alignment.
301 /* Reset all macros within TRACEPOINT_EVENT */
302 #include <lttng/ust-tracepoint-event-reset.h>
304 #undef ctf_integer_ext
305 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
306 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
309 #define ctf_float(_type, _item, _src) \
310 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
312 #undef ctf_array_encoded
313 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
314 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
316 #undef ctf_sequence_encoded
317 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
318 _src_length, _encoding) \
319 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_length_type)); \
320 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
323 #define ctf_string(_item, _src)
326 #define TP_ARGS(...) __VA_ARGS__
329 #define TP_FIELDS(...) __VA_ARGS__
331 #undef TRACEPOINT_EVENT_CLASS
332 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
334 size_t __event_get_align__##_provider##___##_name(_TP_ARGS_PROTO(_args)) \
336 size_t __event_align = 1; \
338 return __event_align; \
341 #include TRACEPOINT_INCLUDE
345 * Stage 5 of tracepoint event generation.
347 * Create the probe function. This function calls event size calculation
348 * and writes event data into the buffer.
351 /* Reset all macros within TRACEPOINT_EVENT */
352 #include <lttng/ust-tracepoint-event-reset.h>
354 #undef ctf_integer_ext
355 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
357 _type __tmp = (_src); \
358 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
359 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
363 #define ctf_float(_type, _item, _src) \
365 _type __tmp = (_src); \
366 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
367 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
370 #undef ctf_array_encoded
371 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
372 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
373 __chan->ops->event_write(&__ctx, _src, sizeof(_type) * (_length));
375 #undef ctf_sequence_encoded
376 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
377 _src_length, _encoding) \
379 _length_type __tmpl = __dynamic_len[__dynamic_len_idx]; \
380 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_length_type));\
381 __chan->ops->event_write(&__ctx, &__tmpl, sizeof(_length_type));\
383 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
384 __chan->ops->event_write(&__ctx, _src, \
385 sizeof(_type) * __get_dynamic_len(dest));
388 #define ctf_string(_item, _src) \
389 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(*(_src))); \
390 __chan->ops->event_write(&__ctx, _src, __get_dynamic_len(dest));
392 /* Beware: this get len actually consumes the len value */
393 #undef __get_dynamic_len
394 #define __get_dynamic_len(field) __dynamic_len[__dynamic_len_idx++]
397 #define TP_ARGS(...) __VA_ARGS__
400 #define TP_FIELDS(...) __VA_ARGS__
402 #undef TRACEPOINT_EVENT_CLASS
403 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
404 static void __event_probe__##_provider##___##_name(_TP_ARGS_DATA_PROTO(_args))\
406 struct ltt_event *__event = __tp_data; \
407 struct ltt_channel *__chan = __event->chan; \
408 struct lttng_ust_lib_ring_buffer_ctx __ctx; \
409 size_t __event_len, __event_align; \
410 size_t __dynamic_len_idx = 0; \
411 size_t __dynamic_len[_TP_ARRAY_SIZE(__event_fields___##_provider##___##_name)]; \
415 (void) __dynamic_len_idx; /* don't warn if unused */ \
416 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->session->active))) \
418 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->enabled))) \
420 if (caa_unlikely(!CMM_ACCESS_ONCE(__event->enabled))) \
422 __event_len = __event_get_size__##_provider##___##_name(__dynamic_len,\
423 _TP_ARGS_DATA_VAR(_args)); \
424 __event_align = __event_get_align__##_provider##___##_name(_TP_ARGS_VAR(_args)); \
425 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
426 __event_align, -1, __chan->handle); \
427 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
431 __chan->ops->event_commit(&__ctx); \
434 #include TRACEPOINT_INCLUDE
436 #undef __get_dynamic_len
439 * Stage 5.1 of tracepoint event generation.
441 * Create probe signature
444 /* Reset all macros within TRACEPOINT_EVENT */
445 #include <lttng/ust-tracepoint-event-reset.h>
448 #define TP_ARGS(...) __VA_ARGS__
450 #define _TP_EXTRACT_STRING2(...) #__VA_ARGS__
452 #undef TRACEPOINT_EVENT_CLASS
453 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
454 const char __tp_event_signature___##_provider##___##_name[] = \
455 _TP_EXTRACT_STRING2(_args);
457 #include TRACEPOINT_INCLUDE
459 #undef _TP_EXTRACT_STRING2
462 * Stage 6 of tracepoint event generation.
464 * Tracepoint loglevel mapping definition generation. We generate a
465 * symbol for each mapping for a provider/event to ensure at most a 1 to
466 * 1 mapping between events and loglevels. If the symbol is repeated,
467 * the compiler will complain.
470 /* Reset all macros within TRACEPOINT_EVENT */
471 #include <lttng/ust-tracepoint-event-reset.h>
473 #undef TRACEPOINT_LOGLEVEL
474 #define TRACEPOINT_LOGLEVEL(__provider, __name, __loglevel) \
475 static const int _loglevel_value___##__provider##___##__name = __loglevel; \
476 static const int *_loglevel___##__provider##___##__name = \
477 &_loglevel_value___##__provider##___##__name;
479 #include TRACEPOINT_INCLUDE
482 * Stage 7.1 of tracepoint event generation.
484 * Create events description structures. We use a weakref because
485 * loglevels are optional. If not declared, the event will point to the
486 * a loglevel that contains NULL.
489 /* Reset all macros within TRACEPOINT_EVENT */
490 #include <lttng/ust-tracepoint-event-reset.h>
492 #undef TRACEPOINT_EVENT_INSTANCE
493 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
495 __ref_loglevel___##_provider##___##_name \
496 __attribute__((weakref ("_loglevel___" #_provider "___" #_name))); \
497 const struct lttng_event_desc __event_desc___##_provider##_##_name = { \
498 .fields = __event_fields___##_provider##___##_template, \
499 .name = #_provider ":" #_name, \
500 .probe_callback = (void (*)(void)) &__event_probe__##_provider##___##_template,\
501 .nr_fields = _TP_ARRAY_SIZE(__event_fields___##_provider##___##_template), \
502 .loglevel = &__ref_loglevel___##_provider##___##_name, \
503 .signature = __tp_event_signature___##_provider##___##_template, \
506 #include TRACEPOINT_INCLUDE
509 * Stage 7.2 of tracepoint event generation.
511 * Create array of events.
514 /* Reset all macros within TRACEPOINT_EVENT */
515 #include <lttng/ust-tracepoint-event-reset.h>
517 #undef TRACEPOINT_EVENT_INSTANCE
518 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
519 &__event_desc___##_provider##_##_name,
521 static const struct lttng_event_desc
*_TP_COMBINE_TOKENS(__event_desc___
, TRACEPOINT_PROVIDER
)[] = {
522 #include TRACEPOINT_INCLUDE
527 * Stage 8 of tracepoint event generation.
529 * Create a toplevel descriptor for the whole probe.
532 /* non-const because list head will be modified when registered. */
533 static struct lttng_probe_desc
_TP_COMBINE_TOKENS(__probe_desc___
, TRACEPOINT_PROVIDER
) = {
534 .provider
= __tp_stringify(TRACEPOINT_PROVIDER
),
535 .event_desc
= _TP_COMBINE_TOKENS(__event_desc___
, TRACEPOINT_PROVIDER
),
536 .nr_events
= _TP_ARRAY_SIZE(_TP_COMBINE_TOKENS(__event_desc___
, TRACEPOINT_PROVIDER
)),
540 * Stage 9 of tracepoint event generation.
542 * Register/unregister probes at module load/unload.
544 * Generate the constructor as an externally visible symbol for use when
545 * linking the probe statically.
548 /* Reset all macros within TRACEPOINT_EVENT */
549 #include <lttng/ust-tracepoint-event-reset.h>
550 static void __attribute__((constructor
))
551 _TP_COMBINE_TOKENS(__lttng_events_init__
, TRACEPOINT_PROVIDER
)(void)
556 * __tracepoint_provider_check_ ## TRACEPOINT_PROVIDER() is a
557 * static inline function that ensures every probe PROVIDER
558 * argument match the provider within which they appear. It
559 * calls empty static inline functions, and therefore has no
560 * runtime effect. However, if it detects an error, a linker
563 _TP_COMBINE_TOKENS(__tracepoint_provider_check_
, TRACEPOINT_PROVIDER
)();
565 fprintf(stderr
, "LTTng-UST: Error (%d) while registering tracepoint probe. Duplicate registration of tracepoint probes having the same name is not allowed.\n", ret
);
570 static void __attribute__((destructor
))
571 _TP_COMBINE_TOKENS(__lttng_events_exit__
, TRACEPOINT_PROVIDER
)(void)
573 ltt_probe_unregister(&_TP_COMBINE_TOKENS(__probe_desc___
, TRACEPOINT_PROVIDER
));
576 int _TP_COMBINE_TOKENS(__tracepoint_provider_
, TRACEPOINT_PROVIDER
);