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>
19 #include <lttng/ust-compiler.h>
23 * TRACEPOINT_EVENT_CLASS declares a class of tracepoints receiving the
24 * same arguments and having the same field layout.
26 * TRACEPOINT_EVENT_INSTANCE declares an instance of a tracepoint, with
27 * its own provider and name. It refers to a class (template).
29 * TRACEPOINT_EVENT declared both a class and an instance and does a
30 * direct mapping from the instance to the class.
33 #undef TRACEPOINT_EVENT
34 #define TRACEPOINT_EVENT(_provider, _name, _args, _fields) \
35 TRACEPOINT_EVENT_CLASS(_provider, _name, \
37 _TP_PARAMS(_fields)) \
38 TRACEPOINT_EVENT_INSTANCE(_provider, _name, _name, \
42 #define _TP_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
44 #define _tp_max_t(type, x, y) \
48 __max1 > __max2 ? __max1: __max2; \
52 * Stage 0 of tracepoint event generation.
54 * Check that each TRACEPOINT_EVENT provider argument match the
55 * TRACEPOINT_PROVIDER by creating dummy callbacks.
58 /* Reset all macros within TRACEPOINT_EVENT */
59 #include <lttng/ust-tracepoint-event-reset.h>
62 void _TP_COMBINE_TOKENS(__tracepoint_provider_mismatch_
, TRACEPOINT_PROVIDER
)(void)
66 #undef TRACEPOINT_EVENT_CLASS
67 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
68 __tracepoint_provider_mismatch_##_provider();
70 #undef TRACEPOINT_EVENT_INSTANCE
71 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
72 __tracepoint_provider_mismatch_##_provider();
74 static __attribute__((unused
))
75 void _TP_COMBINE_TOKENS(__tracepoint_provider_check_
, TRACEPOINT_PROVIDER
)(void)
77 #include TRACEPOINT_INCLUDE
81 * Stage 0.1 of tracepoint event generation.
83 * Check that each TRACEPOINT_EVENT provider:name does not exceed the
84 * tracepoint name length limit.
87 /* Reset all macros within TRACEPOINT_EVENT */
88 #include <lttng/ust-tracepoint-event-reset.h>
90 #undef TRACEPOINT_EVENT_INSTANCE
91 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
93 __tp_name_len_check##_provider##___##_name[LTTNG_UST_SYM_NAME_LEN] \
94 __attribute__((unused)) = \
95 #_provider ":" #_name;
97 #include TRACEPOINT_INCLUDE
100 * Stage 1 of tracepoint event generation.
102 * Create event field type metadata section.
103 * Each event produce an array of fields.
106 /* Reset all macros within TRACEPOINT_EVENT */
107 #include <lttng/ust-tracepoint-event-reset.h>
108 #include <lttng/ust-tracepoint-event-write.h>
109 #include <lttng/ust-tracepoint-event-nowrite.h>
111 #undef _ctf_integer_ext
112 #define _ctf_integer_ext(_type, _item, _src, _byte_order, _base, _nowrite) \
115 .type = __type_integer(_type, _byte_order, _base, none),\
116 .nowrite = _nowrite, \
120 #define _ctf_float(_type, _item, _src, _nowrite) \
123 .type = __type_float(_type), \
124 .nowrite = _nowrite, \
127 #undef _ctf_array_encoded
128 #define _ctf_array_encoded(_type, _item, _src, _length, _encoding, _nowrite) \
133 .atype = atype_array, \
137 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
140 .nowrite = _nowrite, \
143 #undef _ctf_sequence_encoded
144 #define _ctf_sequence_encoded(_type, _item, _src, \
145 _length_type, _src_length, _encoding, _nowrite) \
150 .atype = atype_sequence, \
153 .length_type = __type_integer(_length_type, BYTE_ORDER, 10, none), \
154 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
157 .nowrite = _nowrite, \
161 #define _ctf_string(_item, _src, _nowrite) \
166 .atype = atype_string, \
167 .u.basic.string.encoding = lttng_encode_UTF8, \
169 .nowrite = _nowrite, \
173 #define TP_FIELDS(...) __VA_ARGS__ /* Only one used in this phase */
175 #undef TRACEPOINT_EVENT_CLASS
176 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
177 static const struct lttng_event_field __event_fields___##_provider##___##_name[] = { \
181 #include TRACEPOINT_INCLUDE
184 * Stage 2 of tracepoint event generation.
186 * Create probe callback prototypes.
189 /* Reset all macros within TRACEPOINT_EVENT */
190 #include <lttng/ust-tracepoint-event-reset.h>
193 #define TP_ARGS(...) __VA_ARGS__
195 #undef TRACEPOINT_EVENT_CLASS
196 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
197 static void __event_probe__##_provider##___##_name(_TP_ARGS_DATA_PROTO(_args));
199 #include TRACEPOINT_INCLUDE
202 * Stage 3 of tracepoint event generation.
204 * Create static inline function that calculates event size.
207 /* Reset all macros within TRACEPOINT_EVENT */
208 #include <lttng/ust-tracepoint-event-reset.h>
209 #include <lttng/ust-tracepoint-event-write.h>
211 #undef _ctf_integer_ext
212 #define _ctf_integer_ext(_type, _item, _src, _byte_order, _base, _nowrite) \
213 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
214 __event_len += sizeof(_type);
217 #define _ctf_float(_type, _item, _src, _nowrite) \
218 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
219 __event_len += sizeof(_type);
221 #undef _ctf_array_encoded
222 #define _ctf_array_encoded(_type, _item, _src, _length, _encoding, _nowrite) \
223 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
224 __event_len += sizeof(_type) * (_length);
226 #undef _ctf_sequence_encoded
227 #define _ctf_sequence_encoded(_type, _item, _src, _length_type, \
228 _src_length, _encoding, _nowrite) \
229 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_length_type)); \
230 __event_len += sizeof(_length_type); \
231 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
232 __dynamic_len[__dynamic_len_idx] = (_src_length); \
233 __event_len += sizeof(_type) * __dynamic_len[__dynamic_len_idx]; \
237 #define _ctf_string(_item, _src, _nowrite) \
238 __event_len += __dynamic_len[__dynamic_len_idx++] = strlen(_src) + 1;
241 #define TP_ARGS(...) __VA_ARGS__
244 #define TP_FIELDS(...) __VA_ARGS__
246 #undef TRACEPOINT_EVENT_CLASS
247 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
248 static inline lttng_ust_notrace \
249 size_t __event_get_size__##_provider##___##_name(size_t *__dynamic_len, _TP_ARGS_DATA_PROTO(_args)); \
251 size_t __event_get_size__##_provider##___##_name(size_t *__dynamic_len, _TP_ARGS_DATA_PROTO(_args)) \
253 size_t __event_len = 0; \
254 unsigned int __dynamic_len_idx = 0; \
257 (void) __dynamic_len_idx; /* don't warn if unused */ \
259 return __event_len; \
262 #include TRACEPOINT_INCLUDE
265 * Stage 3.1 of tracepoint event generation.
267 * Create static inline function that layout the filter stack data.
268 * We make both write and nowrite data available to the filter.
271 /* Reset all macros within TRACEPOINT_EVENT */
272 #include <lttng/ust-tracepoint-event-reset.h>
273 #include <lttng/ust-tracepoint-event-write.h>
274 #include <lttng/ust-tracepoint-event-nowrite.h>
276 #undef _ctf_integer_ext
277 #define _ctf_integer_ext(_type, _item, _src, _byte_order, _base, _nowrite) \
278 if (lttng_is_signed_type(_type)) { \
279 int64_t __ctf_tmp_int64 = (int64_t) (_type) (_src); \
280 memcpy(__stack_data, &__ctf_tmp_int64, sizeof(int64_t)); \
282 uint64_t __ctf_tmp_uint64 = (uint64_t) (_type) (_src); \
283 memcpy(__stack_data, &__ctf_tmp_uint64, sizeof(uint64_t)); \
285 __stack_data += sizeof(int64_t);
288 #define _ctf_float(_type, _item, _src, _nowrite) \
290 double __ctf_tmp_double = (double) (_type) (_src); \
291 memcpy(__stack_data, &__ctf_tmp_double, sizeof(double)); \
292 __stack_data += sizeof(double); \
295 #undef _ctf_array_encoded
296 #define _ctf_array_encoded(_type, _item, _src, _length, _encoding, _nowrite) \
298 unsigned long __ctf_tmp_ulong = (unsigned long) (_length); \
299 memcpy(__stack_data, &__ctf_tmp_ulong, sizeof(unsigned long)); \
300 __stack_data += sizeof(unsigned long); \
301 memcpy(__stack_data, &(_src), sizeof(void **)); \
302 __stack_data += sizeof(void **); \
305 #undef _ctf_sequence_encoded
306 #define _ctf_sequence_encoded(_type, _item, _src, _length_type, \
307 _src_length, _encoding, _nowrite) \
309 unsigned long __ctf_tmp_ulong = (unsigned long) (_src_length); \
310 memcpy(__stack_data, &__ctf_tmp_ulong, sizeof(unsigned long)); \
311 __stack_data += sizeof(unsigned long); \
312 memcpy(__stack_data, &(_src), sizeof(void **)); \
313 __stack_data += sizeof(void **); \
317 #define _ctf_string(_item, _src, _nowrite) \
319 memcpy(__stack_data, &(_src), sizeof(void **)); \
320 __stack_data += sizeof(void **); \
324 #define TP_ARGS(...) __VA_ARGS__
327 #define TP_FIELDS(...) __VA_ARGS__
329 #undef TRACEPOINT_EVENT_CLASS
330 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
332 void __event_prepare_filter_stack__##_provider##___##_name(char *__stack_data,\
333 _TP_ARGS_DATA_PROTO(_args)) \
338 #include TRACEPOINT_INCLUDE
343 * Stage 4 of tracepoint event generation.
345 * Create static inline function that calculates event payload alignment.
348 /* Reset all macros within TRACEPOINT_EVENT */
349 #include <lttng/ust-tracepoint-event-reset.h>
350 #include <lttng/ust-tracepoint-event-write.h>
352 #undef _ctf_integer_ext
353 #define _ctf_integer_ext(_type, _item, _src, _byte_order, _base, _nowrite) \
354 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
357 #define _ctf_float(_type, _item, _src, _nowrite) \
358 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
360 #undef _ctf_array_encoded
361 #define _ctf_array_encoded(_type, _item, _src, _length, _encoding, _nowrite) \
362 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
364 #undef _ctf_sequence_encoded
365 #define _ctf_sequence_encoded(_type, _item, _src, _length_type, \
366 _src_length, _encoding, _nowrite) \
367 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_length_type)); \
368 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
371 #define _ctf_string(_item, _src, _nowrite)
374 #define TP_ARGS(...) __VA_ARGS__
377 #define TP_FIELDS(...) __VA_ARGS__
379 #undef TRACEPOINT_EVENT_CLASS
380 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
381 static inline lttng_ust_notrace \
382 size_t __event_get_align__##_provider##___##_name(_TP_ARGS_PROTO(_args)); \
384 size_t __event_get_align__##_provider##___##_name(_TP_ARGS_PROTO(_args)) \
386 size_t __event_align = 1; \
388 return __event_align; \
391 #include TRACEPOINT_INCLUDE
395 * Stage 5 of tracepoint event generation.
397 * Create the probe function. This function calls event size calculation
398 * and writes event data into the buffer.
401 /* Reset all macros within TRACEPOINT_EVENT */
402 #include <lttng/ust-tracepoint-event-reset.h>
403 #include <lttng/ust-tracepoint-event-write.h>
405 #undef _ctf_integer_ext
406 #define _ctf_integer_ext(_type, _item, _src, _byte_order, _base, _nowrite) \
408 _type __tmp = (_src); \
409 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
410 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
414 #define _ctf_float(_type, _item, _src, _nowrite) \
416 _type __tmp = (_src); \
417 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
418 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
421 #undef _ctf_array_encoded
422 #define _ctf_array_encoded(_type, _item, _src, _length, _encoding, _nowrite) \
423 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
424 __chan->ops->event_write(&__ctx, _src, sizeof(_type) * (_length));
426 #undef _ctf_sequence_encoded
427 #define _ctf_sequence_encoded(_type, _item, _src, _length_type, \
428 _src_length, _encoding, _nowrite) \
430 _length_type __tmpl = __stackvar.__dynamic_len[__dynamic_len_idx]; \
431 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_length_type));\
432 __chan->ops->event_write(&__ctx, &__tmpl, sizeof(_length_type));\
434 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
435 __chan->ops->event_write(&__ctx, _src, \
436 sizeof(_type) * __get_dynamic_len(dest));
439 #define _ctf_string(_item, _src, _nowrite) \
440 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(*(_src))); \
441 __chan->ops->event_write(&__ctx, _src, __get_dynamic_len(dest));
443 /* Beware: this get len actually consumes the len value */
444 #undef __get_dynamic_len
445 #define __get_dynamic_len(field) __stackvar.__dynamic_len[__dynamic_len_idx++]
448 #define TP_ARGS(...) __VA_ARGS__
451 #define TP_FIELDS(...) __VA_ARGS__
454 * Using twice size for filter stack data to hold size and pointer for
455 * each field (worse case). For integers, max size required is 64-bit.
456 * Same for double-precision floats. Those fit within
457 * 2*sizeof(unsigned long) for all supported architectures.
459 #undef TRACEPOINT_EVENT_CLASS
460 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
461 static lttng_ust_notrace \
462 void __event_probe__##_provider##___##_name(_TP_ARGS_DATA_PROTO(_args)); \
464 void __event_probe__##_provider##___##_name(_TP_ARGS_DATA_PROTO(_args)) \
466 struct ltt_event *__event = __tp_data; \
467 struct ltt_channel *__chan = __event->chan; \
468 struct lttng_ust_lib_ring_buffer_ctx __ctx; \
469 size_t __event_len, __event_align; \
470 size_t __dynamic_len_idx = 0; \
472 size_t __dynamic_len[_TP_ARRAY_SIZE(__event_fields___##_provider##___##_name)]; \
473 char __filter_stack_data[2 * sizeof(unsigned long) * _TP_ARRAY_SIZE(__event_fields___##_provider##___##_name)]; \
478 (void) __dynamic_len_idx; /* don't warn if unused */ \
479 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->session->active))) \
481 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->enabled))) \
483 if (caa_unlikely(!CMM_ACCESS_ONCE(__event->enabled))) \
485 if (caa_unlikely(__event->filter)) { \
486 __event_prepare_filter_stack__##_provider##___##_name(__stackvar.__filter_stack_data, \
487 _TP_ARGS_DATA_VAR(_args)); \
488 if (caa_likely(!__event->filter(__event->filter_data, __stackvar.__filter_stack_data))) \
491 __event_len = __event_get_size__##_provider##___##_name(__stackvar.__dynamic_len, \
492 _TP_ARGS_DATA_VAR(_args)); \
493 __event_align = __event_get_align__##_provider##___##_name(_TP_ARGS_VAR(_args)); \
494 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
495 __event_align, -1, __chan->handle); \
496 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
500 __chan->ops->event_commit(&__ctx); \
503 #include TRACEPOINT_INCLUDE
505 #undef __get_dynamic_len
508 * Stage 5.1 of tracepoint event generation.
510 * Create probe signature
513 /* Reset all macros within TRACEPOINT_EVENT */
514 #include <lttng/ust-tracepoint-event-reset.h>
517 #define TP_ARGS(...) __VA_ARGS__
519 #define _TP_EXTRACT_STRING2(...) #__VA_ARGS__
521 #undef TRACEPOINT_EVENT_CLASS
522 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
523 const char __tp_event_signature___##_provider##___##_name[] = \
524 _TP_EXTRACT_STRING2(_args);
526 #include TRACEPOINT_INCLUDE
528 #undef _TP_EXTRACT_STRING2
531 * Stage 6 of tracepoint event generation.
533 * Tracepoint loglevel mapping definition generation. We generate a
534 * symbol for each mapping for a provider/event to ensure at most a 1 to
535 * 1 mapping between events and loglevels. If the symbol is repeated,
536 * the compiler will complain.
539 /* Reset all macros within TRACEPOINT_EVENT */
540 #include <lttng/ust-tracepoint-event-reset.h>
542 #undef TRACEPOINT_LOGLEVEL
543 #define TRACEPOINT_LOGLEVEL(__provider, __name, __loglevel) \
544 static const int _loglevel_value___##__provider##___##__name = __loglevel; \
545 static const int *_loglevel___##__provider##___##__name = \
546 &_loglevel_value___##__provider##___##__name;
548 #include TRACEPOINT_INCLUDE
551 * Stage 7.1 of tracepoint event generation.
553 * Create events description structures. We use a weakref because
554 * loglevels are optional. If not declared, the event will point to the
555 * a loglevel that contains NULL.
558 /* Reset all macros within TRACEPOINT_EVENT */
559 #include <lttng/ust-tracepoint-event-reset.h>
561 #undef TRACEPOINT_EVENT_INSTANCE
562 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
564 __ref_loglevel___##_provider##___##_name \
565 __attribute__((weakref ("_loglevel___" #_provider "___" #_name))); \
566 const struct lttng_event_desc __event_desc___##_provider##_##_name = { \
567 .fields = __event_fields___##_provider##___##_template, \
568 .name = #_provider ":" #_name, \
569 .probe_callback = (void (*)(void)) &__event_probe__##_provider##___##_template,\
570 .nr_fields = _TP_ARRAY_SIZE(__event_fields___##_provider##___##_template), \
571 .loglevel = &__ref_loglevel___##_provider##___##_name, \
572 .signature = __tp_event_signature___##_provider##___##_template, \
575 #include TRACEPOINT_INCLUDE
578 * Stage 7.2 of tracepoint event generation.
580 * Create array of events.
583 /* Reset all macros within TRACEPOINT_EVENT */
584 #include <lttng/ust-tracepoint-event-reset.h>
586 #undef TRACEPOINT_EVENT_INSTANCE
587 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
588 &__event_desc___##_provider##_##_name,
590 static const struct lttng_event_desc
*_TP_COMBINE_TOKENS(__event_desc___
, TRACEPOINT_PROVIDER
)[] = {
591 #include TRACEPOINT_INCLUDE
596 * Stage 8 of tracepoint event generation.
598 * Create a toplevel descriptor for the whole probe.
601 /* non-const because list head will be modified when registered. */
602 static struct lttng_probe_desc
_TP_COMBINE_TOKENS(__probe_desc___
, TRACEPOINT_PROVIDER
) = {
603 .provider
= __tp_stringify(TRACEPOINT_PROVIDER
),
604 .event_desc
= _TP_COMBINE_TOKENS(__event_desc___
, TRACEPOINT_PROVIDER
),
605 .nr_events
= _TP_ARRAY_SIZE(_TP_COMBINE_TOKENS(__event_desc___
, TRACEPOINT_PROVIDER
)),
609 * Stage 9 of tracepoint event generation.
611 * Register/unregister probes at module load/unload.
613 * Generate the constructor as an externally visible symbol for use when
614 * linking the probe statically.
617 /* Reset all macros within TRACEPOINT_EVENT */
618 #include <lttng/ust-tracepoint-event-reset.h>
619 static void lttng_ust_notrace
__attribute__((constructor
))
620 _TP_COMBINE_TOKENS(__lttng_events_init__
, TRACEPOINT_PROVIDER
)(void);
622 _TP_COMBINE_TOKENS(__lttng_events_init__
, TRACEPOINT_PROVIDER
)(void)
626 ret
= ltt_probe_register(&_TP_COMBINE_TOKENS(__probe_desc___
, TRACEPOINT_PROVIDER
));
630 static void lttng_ust_notrace
__attribute__((destructor
))
631 _TP_COMBINE_TOKENS(__lttng_events_exit__
, TRACEPOINT_PROVIDER
)(void);
633 _TP_COMBINE_TOKENS(__lttng_events_exit__
, TRACEPOINT_PROVIDER
)(void)
635 ltt_probe_unregister(&_TP_COMBINE_TOKENS(__probe_desc___
, TRACEPOINT_PROVIDER
));
638 int _TP_COMBINE_TOKENS(__tracepoint_provider_
, TRACEPOINT_PROVIDER
);