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 void *__ctf_tmp_ptr = (_src); \
300 memcpy(__stack_data, &__ctf_tmp_ulong, sizeof(unsigned long)); \
301 __stack_data += sizeof(unsigned long); \
302 memcpy(__stack_data, &__ctf_tmp_ptr, sizeof(void **)); \
303 __stack_data += sizeof(void **); \
306 #undef _ctf_sequence_encoded
307 #define _ctf_sequence_encoded(_type, _item, _src, _length_type, \
308 _src_length, _encoding, _nowrite) \
310 unsigned long __ctf_tmp_ulong = (unsigned long) (_src_length); \
311 void *__ctf_tmp_ptr = (_src); \
312 memcpy(__stack_data, &__ctf_tmp_ulong, sizeof(unsigned long)); \
313 __stack_data += sizeof(unsigned long); \
314 memcpy(__stack_data, &__ctf_tmp_ptr, sizeof(void **)); \
315 __stack_data += sizeof(void **); \
319 #define _ctf_string(_item, _src, _nowrite) \
321 void *__ctf_tmp_ptr = (_src); \
322 memcpy(__stack_data, &__ctf_tmp_ptr, sizeof(void **)); \
323 __stack_data += sizeof(void **); \
327 #define TP_ARGS(...) __VA_ARGS__
330 #define TP_FIELDS(...) __VA_ARGS__
332 #undef TRACEPOINT_EVENT_CLASS
333 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
335 void __event_prepare_filter_stack__##_provider##___##_name(char *__stack_data,\
336 _TP_ARGS_DATA_PROTO(_args)) \
341 #include TRACEPOINT_INCLUDE
346 * Stage 4 of tracepoint event generation.
348 * Create static inline function that calculates event payload alignment.
351 /* Reset all macros within TRACEPOINT_EVENT */
352 #include <lttng/ust-tracepoint-event-reset.h>
353 #include <lttng/ust-tracepoint-event-write.h>
355 #undef _ctf_integer_ext
356 #define _ctf_integer_ext(_type, _item, _src, _byte_order, _base, _nowrite) \
357 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
360 #define _ctf_float(_type, _item, _src, _nowrite) \
361 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
363 #undef _ctf_array_encoded
364 #define _ctf_array_encoded(_type, _item, _src, _length, _encoding, _nowrite) \
365 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
367 #undef _ctf_sequence_encoded
368 #define _ctf_sequence_encoded(_type, _item, _src, _length_type, \
369 _src_length, _encoding, _nowrite) \
370 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_length_type)); \
371 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
374 #define _ctf_string(_item, _src, _nowrite)
377 #define TP_ARGS(...) __VA_ARGS__
380 #define TP_FIELDS(...) __VA_ARGS__
382 #undef TRACEPOINT_EVENT_CLASS
383 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
384 static inline lttng_ust_notrace \
385 size_t __event_get_align__##_provider##___##_name(_TP_ARGS_PROTO(_args)); \
387 size_t __event_get_align__##_provider##___##_name(_TP_ARGS_PROTO(_args)) \
389 size_t __event_align = 1; \
391 return __event_align; \
394 #include TRACEPOINT_INCLUDE
398 * Stage 5 of tracepoint event generation.
400 * Create the probe function. This function calls event size calculation
401 * and writes event data into the buffer.
404 /* Reset all macros within TRACEPOINT_EVENT */
405 #include <lttng/ust-tracepoint-event-reset.h>
406 #include <lttng/ust-tracepoint-event-write.h>
408 #undef _ctf_integer_ext
409 #define _ctf_integer_ext(_type, _item, _src, _byte_order, _base, _nowrite) \
411 _type __tmp = (_src); \
412 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
413 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
417 #define _ctf_float(_type, _item, _src, _nowrite) \
419 _type __tmp = (_src); \
420 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
421 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
424 #undef _ctf_array_encoded
425 #define _ctf_array_encoded(_type, _item, _src, _length, _encoding, _nowrite) \
426 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
427 __chan->ops->event_write(&__ctx, _src, sizeof(_type) * (_length));
429 #undef _ctf_sequence_encoded
430 #define _ctf_sequence_encoded(_type, _item, _src, _length_type, \
431 _src_length, _encoding, _nowrite) \
433 _length_type __tmpl = __stackvar.__dynamic_len[__dynamic_len_idx]; \
434 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_length_type));\
435 __chan->ops->event_write(&__ctx, &__tmpl, sizeof(_length_type));\
437 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
438 __chan->ops->event_write(&__ctx, _src, \
439 sizeof(_type) * __get_dynamic_len(dest));
442 #define _ctf_string(_item, _src, _nowrite) \
443 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(*(_src))); \
444 __chan->ops->event_write(&__ctx, _src, __get_dynamic_len(dest));
446 /* Beware: this get len actually consumes the len value */
447 #undef __get_dynamic_len
448 #define __get_dynamic_len(field) __stackvar.__dynamic_len[__dynamic_len_idx++]
451 #define TP_ARGS(...) __VA_ARGS__
454 #define TP_FIELDS(...) __VA_ARGS__
457 * Using twice size for filter stack data to hold size and pointer for
458 * each field (worse case). For integers, max size required is 64-bit.
459 * Same for double-precision floats. Those fit within
460 * 2*sizeof(unsigned long) for all supported architectures.
462 #undef TRACEPOINT_EVENT_CLASS
463 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
464 static lttng_ust_notrace \
465 void __event_probe__##_provider##___##_name(_TP_ARGS_DATA_PROTO(_args)); \
467 void __event_probe__##_provider##___##_name(_TP_ARGS_DATA_PROTO(_args)) \
469 struct ltt_event *__event = __tp_data; \
470 struct ltt_channel *__chan = __event->chan; \
471 struct lttng_ust_lib_ring_buffer_ctx __ctx; \
472 size_t __event_len, __event_align; \
473 size_t __dynamic_len_idx = 0; \
475 size_t __dynamic_len[_TP_ARRAY_SIZE(__event_fields___##_provider##___##_name)]; \
476 char __filter_stack_data[2 * sizeof(unsigned long) * _TP_ARRAY_SIZE(__event_fields___##_provider##___##_name)]; \
481 (void) __dynamic_len_idx; /* don't warn if unused */ \
482 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->session->active))) \
484 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->enabled))) \
486 if (caa_unlikely(!CMM_ACCESS_ONCE(__event->enabled))) \
488 if (caa_unlikely(__event->filter)) { \
489 __event_prepare_filter_stack__##_provider##___##_name(__stackvar.__filter_stack_data, \
490 _TP_ARGS_DATA_VAR(_args)); \
491 if (caa_likely(!__event->filter(__event->filter_data, __stackvar.__filter_stack_data))) \
494 __event_len = __event_get_size__##_provider##___##_name(__stackvar.__dynamic_len, \
495 _TP_ARGS_DATA_VAR(_args)); \
496 __event_align = __event_get_align__##_provider##___##_name(_TP_ARGS_VAR(_args)); \
497 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
498 __event_align, -1, __chan->handle); \
499 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
503 __chan->ops->event_commit(&__ctx); \
506 #include TRACEPOINT_INCLUDE
508 #undef __get_dynamic_len
511 * Stage 5.1 of tracepoint event generation.
513 * Create probe signature
516 /* Reset all macros within TRACEPOINT_EVENT */
517 #include <lttng/ust-tracepoint-event-reset.h>
520 #define TP_ARGS(...) __VA_ARGS__
522 #define _TP_EXTRACT_STRING2(...) #__VA_ARGS__
524 #undef TRACEPOINT_EVENT_CLASS
525 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
526 const char __tp_event_signature___##_provider##___##_name[] = \
527 _TP_EXTRACT_STRING2(_args);
529 #include TRACEPOINT_INCLUDE
531 #undef _TP_EXTRACT_STRING2
534 * Stage 6 of tracepoint event generation.
536 * Tracepoint loglevel mapping definition generation. We generate a
537 * symbol for each mapping for a provider/event to ensure at most a 1 to
538 * 1 mapping between events and loglevels. If the symbol is repeated,
539 * the compiler will complain.
542 /* Reset all macros within TRACEPOINT_EVENT */
543 #include <lttng/ust-tracepoint-event-reset.h>
545 #undef TRACEPOINT_LOGLEVEL
546 #define TRACEPOINT_LOGLEVEL(__provider, __name, __loglevel) \
547 static const int _loglevel_value___##__provider##___##__name = __loglevel; \
548 static const int *_loglevel___##__provider##___##__name = \
549 &_loglevel_value___##__provider##___##__name;
551 #include TRACEPOINT_INCLUDE
554 * Stage 6.1 of tracepoint event generation.
556 * Tracepoint UML URI info.
559 /* Reset all macros within TRACEPOINT_EVENT */
560 #include <lttng/ust-tracepoint-event-reset.h>
562 #undef TRACEPOINT_MODEL_EMF_URI
563 #define TRACEPOINT_MODEL_EMF_URI(__provider, __name, __uri) \
564 static const char *_model_emf_uri___##__provider##___##__name = __uri;
566 #include TRACEPOINT_INCLUDE
569 * Stage 7.1 of tracepoint event generation.
571 * Create events description structures. We use a weakref because
572 * loglevels are optional. If not declared, the event will point to the
573 * a loglevel that contains NULL.
576 /* Reset all macros within TRACEPOINT_EVENT */
577 #include <lttng/ust-tracepoint-event-reset.h>
579 #undef TRACEPOINT_EVENT_INSTANCE
580 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
582 __ref_loglevel___##_provider##___##_name \
583 __attribute__((weakref ("_loglevel___" #_provider "___" #_name))); \
584 static const char * \
585 __ref_model_emf_uri___##_provider##___##_name \
586 __attribute__((weakref ("_model_emf_uri___" #_provider "___" #_name)));\
587 const struct lttng_event_desc __event_desc___##_provider##_##_name = { \
588 .fields = __event_fields___##_provider##___##_template, \
589 .name = #_provider ":" #_name, \
590 .probe_callback = (void (*)(void)) &__event_probe__##_provider##___##_template,\
591 .nr_fields = _TP_ARRAY_SIZE(__event_fields___##_provider##___##_template), \
592 .loglevel = &__ref_loglevel___##_provider##___##_name, \
593 .signature = __tp_event_signature___##_provider##___##_template, \
594 .u.ext.model_emf_uri = &__ref_model_emf_uri___##_provider##___##_name, \
597 #include TRACEPOINT_INCLUDE
600 * Stage 7.2 of tracepoint event generation.
602 * Create array of events.
605 /* Reset all macros within TRACEPOINT_EVENT */
606 #include <lttng/ust-tracepoint-event-reset.h>
608 #undef TRACEPOINT_EVENT_INSTANCE
609 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
610 &__event_desc___##_provider##_##_name,
612 static const struct lttng_event_desc
*_TP_COMBINE_TOKENS(__event_desc___
, TRACEPOINT_PROVIDER
)[] = {
613 #include TRACEPOINT_INCLUDE
618 * Stage 8 of tracepoint event generation.
620 * Create a toplevel descriptor for the whole probe.
623 /* non-const because list head will be modified when registered. */
624 static struct lttng_probe_desc
_TP_COMBINE_TOKENS(__probe_desc___
, TRACEPOINT_PROVIDER
) = {
625 .provider
= __tp_stringify(TRACEPOINT_PROVIDER
),
626 .event_desc
= _TP_COMBINE_TOKENS(__event_desc___
, TRACEPOINT_PROVIDER
),
627 .nr_events
= _TP_ARRAY_SIZE(_TP_COMBINE_TOKENS(__event_desc___
, TRACEPOINT_PROVIDER
)),
631 * Stage 9 of tracepoint event generation.
633 * Register/unregister probes at module load/unload.
635 * Generate the constructor as an externally visible symbol for use when
636 * linking the probe statically.
639 /* Reset all macros within TRACEPOINT_EVENT */
640 #include <lttng/ust-tracepoint-event-reset.h>
641 static void lttng_ust_notrace
__attribute__((constructor
))
642 _TP_COMBINE_TOKENS(__lttng_events_init__
, TRACEPOINT_PROVIDER
)(void);
644 _TP_COMBINE_TOKENS(__lttng_events_init__
, TRACEPOINT_PROVIDER
)(void)
648 ret
= ltt_probe_register(&_TP_COMBINE_TOKENS(__probe_desc___
, TRACEPOINT_PROVIDER
));
652 static void lttng_ust_notrace
__attribute__((destructor
))
653 _TP_COMBINE_TOKENS(__lttng_events_exit__
, TRACEPOINT_PROVIDER
)(void);
655 _TP_COMBINE_TOKENS(__lttng_events_exit__
, TRACEPOINT_PROVIDER
)(void)
657 ltt_probe_unregister(&_TP_COMBINE_TOKENS(__probe_desc___
, TRACEPOINT_PROVIDER
));
660 int _TP_COMBINE_TOKENS(__tracepoint_provider_
, TRACEPOINT_PROVIDER
);