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(...) __VA_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(...) __VA_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(...) __VA_ARGS__
277 #define TP_FIELDS(...) __VA_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 3.1 of tracepoint event generation.
297 * Create static inline function that layout the filter stack data.
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 if (lttng_is_signed_type(_type)) \
306 *(int64_t *) __stack_data = (int64_t) (_type) (_src); \
308 *(uint64_t *) __stack_data = (uint64_t) (_type) (_src); \
309 __stack_data += sizeof(int64_t);
312 #define ctf_float(_type, _item, _src) \
313 *(double *) __stack_data = (double) (_type) (_src); \
314 __stack_data += sizeof(double);
316 #undef ctf_array_encoded
317 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
318 *(unsigned long *) __stack_data = (unsigned long) (_length); \
319 __stack_data += sizeof(unsigned long); \
320 *(const void **) __stack_data = (_src); \
321 __stack_data += sizeof(void *);
323 #undef ctf_sequence_encoded
324 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
325 _src_length, _encoding) \
326 *(unsigned long *) __stack_data = (unsigned long) (_src_length); \
327 __stack_data += sizeof(unsigned long); \
328 *(const void **) __stack_data = (_src); \
329 __stack_data += sizeof(void *);
332 #define ctf_string(_item, _src) \
333 *(const void **) __stack_data = (_src); \
334 __stack_data += sizeof(void *);
337 #define TP_ARGS(...) __VA_ARGS__
340 #define TP_FIELDS(...) __VA_ARGS__
342 #undef TRACEPOINT_EVENT_CLASS
343 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
345 void __event_prepare_filter_stack__##_provider##___##_name(char *__stack_data,\
346 _TP_ARGS_DATA_PROTO(_args)) \
351 #include TRACEPOINT_INCLUDE
356 * Stage 4 of tracepoint event generation.
358 * Create static inline function that calculates event payload alignment.
361 /* Reset all macros within TRACEPOINT_EVENT */
362 #include <lttng/ust-tracepoint-event-reset.h>
364 #undef ctf_integer_ext
365 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
366 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
369 #define ctf_float(_type, _item, _src) \
370 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
372 #undef ctf_array_encoded
373 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
374 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
376 #undef ctf_sequence_encoded
377 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
378 _src_length, _encoding) \
379 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_length_type)); \
380 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
383 #define ctf_string(_item, _src)
386 #define TP_ARGS(...) __VA_ARGS__
389 #define TP_FIELDS(...) __VA_ARGS__
391 #undef TRACEPOINT_EVENT_CLASS
392 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
394 size_t __event_get_align__##_provider##___##_name(_TP_ARGS_PROTO(_args)) \
396 size_t __event_align = 1; \
398 return __event_align; \
401 #include TRACEPOINT_INCLUDE
405 * Stage 5 of tracepoint event generation.
407 * Create the probe function. This function calls event size calculation
408 * and writes event data into the buffer.
411 /* Reset all macros within TRACEPOINT_EVENT */
412 #include <lttng/ust-tracepoint-event-reset.h>
414 #undef ctf_integer_ext
415 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
417 _type __tmp = (_src); \
418 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
419 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
423 #define ctf_float(_type, _item, _src) \
425 _type __tmp = (_src); \
426 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
427 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
430 #undef ctf_array_encoded
431 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
432 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
433 __chan->ops->event_write(&__ctx, _src, sizeof(_type) * (_length));
435 #undef ctf_sequence_encoded
436 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
437 _src_length, _encoding) \
439 _length_type __tmpl = __stackvar.__dynamic_len[__dynamic_len_idx]; \
440 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_length_type));\
441 __chan->ops->event_write(&__ctx, &__tmpl, sizeof(_length_type));\
443 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
444 __chan->ops->event_write(&__ctx, _src, \
445 sizeof(_type) * __get_dynamic_len(dest));
448 #define ctf_string(_item, _src) \
449 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(*(_src))); \
450 __chan->ops->event_write(&__ctx, _src, __get_dynamic_len(dest));
452 /* Beware: this get len actually consumes the len value */
453 #undef __get_dynamic_len
454 #define __get_dynamic_len(field) __stackvar.__dynamic_len[__dynamic_len_idx++]
457 #define TP_ARGS(...) __VA_ARGS__
460 #define TP_FIELDS(...) __VA_ARGS__
463 * Using twice size for filter stack data to hold size and pointer for
464 * each field (worse case). For integers, max size required is 64-bit.
465 * Same for double-precision floats. Those fit within
466 * 2*sizeof(unsigned long) for all supported architectures.
468 #undef TRACEPOINT_EVENT_CLASS
469 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
470 static void __event_probe__##_provider##___##_name(_TP_ARGS_DATA_PROTO(_args))\
472 struct ltt_event *__event = __tp_data; \
473 struct ltt_channel *__chan = __event->chan; \
474 struct lttng_ust_lib_ring_buffer_ctx __ctx; \
475 size_t __event_len, __event_align; \
476 size_t __dynamic_len_idx = 0; \
478 size_t __dynamic_len[_TP_ARRAY_SIZE(__event_fields___##_provider##___##_name)]; \
479 char __filter_stack_data[2 * sizeof(unsigned long) * _TP_ARRAY_SIZE(__event_fields___##_provider##___##_name)]; \
484 (void) __dynamic_len_idx; /* don't warn if unused */ \
485 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->session->active))) \
487 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->enabled))) \
489 if (caa_unlikely(!CMM_ACCESS_ONCE(__event->enabled))) \
491 if (caa_unlikely(__event->filter)) { \
492 __event_prepare_filter_stack__##_provider##___##_name(__stackvar.__filter_stack_data, \
493 _TP_ARGS_DATA_VAR(_args)); \
494 if (caa_likely(!__event->filter(__event->filter_data, __stackvar.__filter_stack_data))) \
497 __event_len = __event_get_size__##_provider##___##_name(__stackvar.__dynamic_len, \
498 _TP_ARGS_DATA_VAR(_args)); \
499 __event_align = __event_get_align__##_provider##___##_name(_TP_ARGS_VAR(_args)); \
500 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
501 __event_align, -1, __chan->handle); \
502 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
506 __chan->ops->event_commit(&__ctx); \
509 #include TRACEPOINT_INCLUDE
511 #undef __get_dynamic_len
514 * Stage 5.1 of tracepoint event generation.
516 * Create probe signature
519 /* Reset all macros within TRACEPOINT_EVENT */
520 #include <lttng/ust-tracepoint-event-reset.h>
523 #define TP_ARGS(...) __VA_ARGS__
525 #define _TP_EXTRACT_STRING2(...) #__VA_ARGS__
527 #undef TRACEPOINT_EVENT_CLASS
528 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
529 const char __tp_event_signature___##_provider##___##_name[] = \
530 _TP_EXTRACT_STRING2(_args);
532 #include TRACEPOINT_INCLUDE
534 #undef _TP_EXTRACT_STRING2
537 * Stage 6 of tracepoint event generation.
539 * Tracepoint loglevel mapping definition generation. We generate a
540 * symbol for each mapping for a provider/event to ensure at most a 1 to
541 * 1 mapping between events and loglevels. If the symbol is repeated,
542 * the compiler will complain.
545 /* Reset all macros within TRACEPOINT_EVENT */
546 #include <lttng/ust-tracepoint-event-reset.h>
548 #undef TRACEPOINT_LOGLEVEL
549 #define TRACEPOINT_LOGLEVEL(__provider, __name, __loglevel) \
550 static const int _loglevel_value___##__provider##___##__name = __loglevel; \
551 static const int *_loglevel___##__provider##___##__name = \
552 &_loglevel_value___##__provider##___##__name;
554 #include TRACEPOINT_INCLUDE
557 * Stage 7.1 of tracepoint event generation.
559 * Create events description structures. We use a weakref because
560 * loglevels are optional. If not declared, the event will point to the
561 * a loglevel that contains NULL.
564 /* Reset all macros within TRACEPOINT_EVENT */
565 #include <lttng/ust-tracepoint-event-reset.h>
567 #undef TRACEPOINT_EVENT_INSTANCE
568 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
570 __ref_loglevel___##_provider##___##_name \
571 __attribute__((weakref ("_loglevel___" #_provider "___" #_name))); \
572 const struct lttng_event_desc __event_desc___##_provider##_##_name = { \
573 .fields = __event_fields___##_provider##___##_template, \
574 .name = #_provider ":" #_name, \
575 .probe_callback = (void (*)(void)) &__event_probe__##_provider##___##_template,\
576 .nr_fields = _TP_ARRAY_SIZE(__event_fields___##_provider##___##_template), \
577 .loglevel = &__ref_loglevel___##_provider##___##_name, \
578 .signature = __tp_event_signature___##_provider##___##_template, \
581 #include TRACEPOINT_INCLUDE
584 * Stage 7.2 of tracepoint event generation.
586 * Create array of events.
589 /* Reset all macros within TRACEPOINT_EVENT */
590 #include <lttng/ust-tracepoint-event-reset.h>
592 #undef TRACEPOINT_EVENT_INSTANCE
593 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
594 &__event_desc___##_provider##_##_name,
596 static const struct lttng_event_desc
*_TP_COMBINE_TOKENS(__event_desc___
, TRACEPOINT_PROVIDER
)[] = {
597 #include TRACEPOINT_INCLUDE
602 * Stage 8 of tracepoint event generation.
604 * Create a toplevel descriptor for the whole probe.
607 /* non-const because list head will be modified when registered. */
608 static struct lttng_probe_desc
_TP_COMBINE_TOKENS(__probe_desc___
, TRACEPOINT_PROVIDER
) = {
609 .provider
= __tp_stringify(TRACEPOINT_PROVIDER
),
610 .event_desc
= _TP_COMBINE_TOKENS(__event_desc___
, TRACEPOINT_PROVIDER
),
611 .nr_events
= _TP_ARRAY_SIZE(_TP_COMBINE_TOKENS(__event_desc___
, TRACEPOINT_PROVIDER
)),
615 * Stage 9 of tracepoint event generation.
617 * Register/unregister probes at module load/unload.
619 * Generate the constructor as an externally visible symbol for use when
620 * linking the probe statically.
623 /* Reset all macros within TRACEPOINT_EVENT */
624 #include <lttng/ust-tracepoint-event-reset.h>
625 static void __attribute__((constructor
))
626 _TP_COMBINE_TOKENS(__lttng_events_init__
, TRACEPOINT_PROVIDER
)(void)
630 ret
= ltt_probe_register(&_TP_COMBINE_TOKENS(__probe_desc___
, TRACEPOINT_PROVIDER
));
634 static void __attribute__((destructor
))
635 _TP_COMBINE_TOKENS(__lttng_events_exit__
, TRACEPOINT_PROVIDER
)(void)
637 ltt_probe_unregister(&_TP_COMBINE_TOKENS(__probe_desc___
, TRACEPOINT_PROVIDER
));
640 int _TP_COMBINE_TOKENS(__tracepoint_provider_
, TRACEPOINT_PROVIDER
);