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 * TRACEPOINT_EVENT_CLASS declares a class of tracepoints receiving the
23 * same arguments and having the same field layout.
25 * TRACEPOINT_EVENT_INSTANCE declares an instance of a tracepoint, with
26 * its own provider and name. It refers to a class (template).
28 * TRACEPOINT_EVENT declared both a class and an instance and does a
29 * direct mapping from the instance to the class.
32 #undef TRACEPOINT_EVENT
33 #define TRACEPOINT_EVENT(_provider, _name, _args, _fields) \
34 TRACEPOINT_EVENT_CLASS(_provider, _name, \
36 _TP_PARAMS(_fields)) \
37 TRACEPOINT_EVENT_INSTANCE(_provider, _name, _name, \
41 #define _TP_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
43 #define _tp_max_t(type, x, y) \
47 __max1 > __max2 ? __max1: __max2; \
51 * Stage 0 of tracepoint event generation.
53 * Check that each TRACEPOINT_EVENT provider argument match the
54 * TRACEPOINT_PROVIDER by creating dummy callbacks.
57 /* Reset all macros within TRACEPOINT_EVENT */
58 #include <lttng/ust-tracepoint-event-reset.h>
61 void _TP_COMBINE_TOKENS(__tracepoint_provider_mismatch_
, TRACEPOINT_PROVIDER
)(void)
65 #undef TRACEPOINT_EVENT_CLASS
66 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
67 __tracepoint_provider_mismatch_##_provider();
69 #undef TRACEPOINT_EVENT_INSTANCE
70 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
71 __tracepoint_provider_mismatch_##_provider();
73 static __attribute__((unused
))
74 void _TP_COMBINE_TOKENS(__tracepoint_provider_check_
, TRACEPOINT_PROVIDER
)(void)
76 #include TRACEPOINT_INCLUDE
80 * Stage 0.1 of tracepoint event generation.
82 * Check that each TRACEPOINT_EVENT provider:name does not exceed the
83 * tracepoint name length limit.
86 /* Reset all macros within TRACEPOINT_EVENT */
87 #include <lttng/ust-tracepoint-event-reset.h>
89 #undef TRACEPOINT_EVENT_INSTANCE
90 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
92 __tp_name_len_check##_provider##___##_name[LTTNG_UST_SYM_NAME_LEN] \
93 __attribute__((unused)) = \
94 #_provider ":" #_name;
96 #include TRACEPOINT_INCLUDE
99 * Stage 1 of tracepoint event generation.
101 * Create event field type metadata section.
102 * Each event produce an array of fields.
105 /* Reset all macros within TRACEPOINT_EVENT */
106 #include <lttng/ust-tracepoint-event-reset.h>
107 #include <lttng/ust-tracepoint-event-write.h>
108 #include <lttng/ust-tracepoint-event-nowrite.h>
110 #undef _ctf_integer_ext
111 #define _ctf_integer_ext(_type, _item, _src, _byte_order, _base, _written) \
114 .type = __type_integer(_type, _byte_order, _base, none),\
115 .written = _written, \
119 #define _ctf_float(_type, _item, _src, _written) \
122 .type = __type_float(_type), \
123 .written = _written, \
126 #undef _ctf_array_encoded
127 #define _ctf_array_encoded(_type, _item, _src, _length, _encoding, _written) \
132 .atype = atype_array, \
136 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
139 .written = _written, \
142 #undef _ctf_sequence_encoded
143 #define _ctf_sequence_encoded(_type, _item, _src, \
144 _length_type, _src_length, _encoding, _written) \
149 .atype = atype_sequence, \
152 .length_type = __type_integer(_length_type, BYTE_ORDER, 10, none), \
153 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
156 .written = _written, \
160 #define _ctf_string(_item, _src, _written) \
165 .atype = atype_string, \
166 .u.basic.string.encoding = lttng_encode_UTF8, \
168 .written = _written, \
172 #define TP_FIELDS(...) __VA_ARGS__ /* Only one used in this phase */
174 #undef TRACEPOINT_EVENT_CLASS
175 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
176 static const struct lttng_event_field __event_fields___##_provider##___##_name[] = { \
180 #include TRACEPOINT_INCLUDE
183 * Stage 2 of tracepoint event generation.
185 * Create probe callback prototypes.
188 /* Reset all macros within TRACEPOINT_EVENT */
189 #include <lttng/ust-tracepoint-event-reset.h>
192 #define TP_ARGS(...) __VA_ARGS__
194 #undef TRACEPOINT_EVENT_CLASS
195 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
196 static void __event_probe__##_provider##___##_name(_TP_ARGS_DATA_PROTO(_args));
198 #include TRACEPOINT_INCLUDE
201 * Stage 3 of tracepoint event generation.
203 * Create static inline function that calculates event size.
206 /* Reset all macros within TRACEPOINT_EVENT */
207 #include <lttng/ust-tracepoint-event-reset.h>
208 #include <lttng/ust-tracepoint-event-write.h>
210 #undef _ctf_integer_ext
211 #define _ctf_integer_ext(_type, _item, _src, _byte_order, _base, _written) \
212 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
213 __event_len += sizeof(_type);
216 #define _ctf_float(_type, _item, _src, _written) \
217 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
218 __event_len += sizeof(_type);
220 #undef _ctf_array_encoded
221 #define _ctf_array_encoded(_type, _item, _src, _length, _encoding, _written) \
222 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
223 __event_len += sizeof(_type) * (_length);
225 #undef _ctf_sequence_encoded
226 #define _ctf_sequence_encoded(_type, _item, _src, _length_type, \
227 _src_length, _encoding, _written) \
228 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_length_type)); \
229 __event_len += sizeof(_length_type); \
230 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
231 __dynamic_len[__dynamic_len_idx] = (_src_length); \
232 __event_len += sizeof(_type) * __dynamic_len[__dynamic_len_idx]; \
236 #define _ctf_string(_item, _src, _written) \
237 __event_len += __dynamic_len[__dynamic_len_idx++] = strlen(_src) + 1;
240 #define TP_ARGS(...) __VA_ARGS__
243 #define TP_FIELDS(...) __VA_ARGS__
245 #undef TRACEPOINT_EVENT_CLASS
246 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
247 static inline size_t __event_get_size__##_provider##___##_name(size_t *__dynamic_len, _TP_ARGS_DATA_PROTO(_args)) \
249 size_t __event_len = 0; \
250 unsigned int __dynamic_len_idx = 0; \
253 (void) __dynamic_len_idx; /* don't warn if unused */ \
255 return __event_len; \
258 #include TRACEPOINT_INCLUDE
261 * Stage 3.1 of tracepoint event generation.
263 * Create static inline function that layout the filter stack data.
264 * We make both write and nowrite data available to the filter.
267 /* Reset all macros within TRACEPOINT_EVENT */
268 #include <lttng/ust-tracepoint-event-reset.h>
269 #include <lttng/ust-tracepoint-event-write.h>
270 #include <lttng/ust-tracepoint-event-nowrite.h>
272 #undef _ctf_integer_ext
273 #define _ctf_integer_ext(_type, _item, _src, _byte_order, _base, _written) \
274 if (lttng_is_signed_type(_type)) \
275 *(int64_t *) __stack_data = (int64_t) (_type) (_src); \
277 *(uint64_t *) __stack_data = (uint64_t) (_type) (_src); \
278 __stack_data += sizeof(int64_t);
281 #define _ctf_float(_type, _item, _src, _written) \
282 *(double *) __stack_data = (double) (_type) (_src); \
283 __stack_data += sizeof(double);
285 #undef _ctf_array_encoded
286 #define _ctf_array_encoded(_type, _item, _src, _length, _encoding, _written) \
287 *(unsigned long *) __stack_data = (unsigned long) (_length); \
288 __stack_data += sizeof(unsigned long); \
289 *(const void **) __stack_data = (_src); \
290 __stack_data += sizeof(void *);
292 #undef _ctf_sequence_encoded
293 #define _ctf_sequence_encoded(_type, _item, _src, _length_type, \
294 _src_length, _encoding, _written) \
295 *(unsigned long *) __stack_data = (unsigned long) (_src_length); \
296 __stack_data += sizeof(unsigned long); \
297 *(const void **) __stack_data = (_src); \
298 __stack_data += sizeof(void *);
301 #define _ctf_string(_item, _src, _written) \
302 *(const void **) __stack_data = (_src); \
303 __stack_data += sizeof(void *);
306 #define TP_ARGS(...) __VA_ARGS__
309 #define TP_FIELDS(...) __VA_ARGS__
311 #undef TRACEPOINT_EVENT_CLASS
312 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
314 void __event_prepare_filter_stack__##_provider##___##_name(char *__stack_data,\
315 _TP_ARGS_DATA_PROTO(_args)) \
320 #include TRACEPOINT_INCLUDE
325 * Stage 4 of tracepoint event generation.
327 * Create static inline function that calculates event payload alignment.
330 /* Reset all macros within TRACEPOINT_EVENT */
331 #include <lttng/ust-tracepoint-event-reset.h>
332 #include <lttng/ust-tracepoint-event-write.h>
334 #undef _ctf_integer_ext
335 #define _ctf_integer_ext(_type, _item, _src, _byte_order, _base, _written) \
336 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
339 #define _ctf_float(_type, _item, _src, _written) \
340 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
342 #undef _ctf_array_encoded
343 #define _ctf_array_encoded(_type, _item, _src, _length, _encoding, _written) \
344 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
346 #undef _ctf_sequence_encoded
347 #define _ctf_sequence_encoded(_type, _item, _src, _length_type, \
348 _src_length, _encoding, _written) \
349 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_length_type)); \
350 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
353 #define _ctf_string(_item, _src, _written)
356 #define TP_ARGS(...) __VA_ARGS__
359 #define TP_FIELDS(...) __VA_ARGS__
361 #undef TRACEPOINT_EVENT_CLASS
362 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
364 size_t __event_get_align__##_provider##___##_name(_TP_ARGS_PROTO(_args)) \
366 size_t __event_align = 1; \
368 return __event_align; \
371 #include TRACEPOINT_INCLUDE
375 * Stage 5 of tracepoint event generation.
377 * Create the probe function. This function calls event size calculation
378 * and writes event data into the buffer.
381 /* Reset all macros within TRACEPOINT_EVENT */
382 #include <lttng/ust-tracepoint-event-reset.h>
383 #include <lttng/ust-tracepoint-event-write.h>
385 #undef _ctf_integer_ext
386 #define _ctf_integer_ext(_type, _item, _src, _byte_order, _base, _written) \
388 _type __tmp = (_src); \
389 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
390 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
394 #define _ctf_float(_type, _item, _src, _written) \
396 _type __tmp = (_src); \
397 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
398 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
401 #undef _ctf_array_encoded
402 #define _ctf_array_encoded(_type, _item, _src, _length, _encoding, _written) \
403 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
404 __chan->ops->event_write(&__ctx, _src, sizeof(_type) * (_length));
406 #undef _ctf_sequence_encoded
407 #define _ctf_sequence_encoded(_type, _item, _src, _length_type, \
408 _src_length, _encoding, _written) \
410 _length_type __tmpl = __stackvar.__dynamic_len[__dynamic_len_idx]; \
411 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_length_type));\
412 __chan->ops->event_write(&__ctx, &__tmpl, sizeof(_length_type));\
414 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
415 __chan->ops->event_write(&__ctx, _src, \
416 sizeof(_type) * __get_dynamic_len(dest));
419 #define _ctf_string(_item, _src, _written) \
420 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(*(_src))); \
421 __chan->ops->event_write(&__ctx, _src, __get_dynamic_len(dest));
423 /* Beware: this get len actually consumes the len value */
424 #undef __get_dynamic_len
425 #define __get_dynamic_len(field) __stackvar.__dynamic_len[__dynamic_len_idx++]
428 #define TP_ARGS(...) __VA_ARGS__
431 #define TP_FIELDS(...) __VA_ARGS__
434 * Using twice size for filter stack data to hold size and pointer for
435 * each field (worse case). For integers, max size required is 64-bit.
436 * Same for double-precision floats. Those fit within
437 * 2*sizeof(unsigned long) for all supported architectures.
439 #undef TRACEPOINT_EVENT_CLASS
440 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
441 static void __event_probe__##_provider##___##_name(_TP_ARGS_DATA_PROTO(_args))\
443 struct ltt_event *__event = __tp_data; \
444 struct ltt_channel *__chan = __event->chan; \
445 struct lttng_ust_lib_ring_buffer_ctx __ctx; \
446 size_t __event_len, __event_align; \
447 size_t __dynamic_len_idx = 0; \
449 size_t __dynamic_len[_TP_ARRAY_SIZE(__event_fields___##_provider##___##_name)]; \
450 char __filter_stack_data[2 * sizeof(unsigned long) * _TP_ARRAY_SIZE(__event_fields___##_provider##___##_name)]; \
455 (void) __dynamic_len_idx; /* don't warn if unused */ \
456 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->session->active))) \
458 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->enabled))) \
460 if (caa_unlikely(!CMM_ACCESS_ONCE(__event->enabled))) \
462 if (caa_unlikely(__event->filter)) { \
463 __event_prepare_filter_stack__##_provider##___##_name(__stackvar.__filter_stack_data, \
464 _TP_ARGS_DATA_VAR(_args)); \
465 if (caa_likely(!__event->filter(__event->filter_data, __stackvar.__filter_stack_data))) \
468 __event_len = __event_get_size__##_provider##___##_name(__stackvar.__dynamic_len, \
469 _TP_ARGS_DATA_VAR(_args)); \
470 __event_align = __event_get_align__##_provider##___##_name(_TP_ARGS_VAR(_args)); \
471 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
472 __event_align, -1, __chan->handle); \
473 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
477 __chan->ops->event_commit(&__ctx); \
480 #include TRACEPOINT_INCLUDE
482 #undef __get_dynamic_len
485 * Stage 5.1 of tracepoint event generation.
487 * Create probe signature
490 /* Reset all macros within TRACEPOINT_EVENT */
491 #include <lttng/ust-tracepoint-event-reset.h>
494 #define TP_ARGS(...) __VA_ARGS__
496 #define _TP_EXTRACT_STRING2(...) #__VA_ARGS__
498 #undef TRACEPOINT_EVENT_CLASS
499 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
500 const char __tp_event_signature___##_provider##___##_name[] = \
501 _TP_EXTRACT_STRING2(_args);
503 #include TRACEPOINT_INCLUDE
505 #undef _TP_EXTRACT_STRING2
508 * Stage 6 of tracepoint event generation.
510 * Tracepoint loglevel mapping definition generation. We generate a
511 * symbol for each mapping for a provider/event to ensure at most a 1 to
512 * 1 mapping between events and loglevels. If the symbol is repeated,
513 * the compiler will complain.
516 /* Reset all macros within TRACEPOINT_EVENT */
517 #include <lttng/ust-tracepoint-event-reset.h>
519 #undef TRACEPOINT_LOGLEVEL
520 #define TRACEPOINT_LOGLEVEL(__provider, __name, __loglevel) \
521 static const int _loglevel_value___##__provider##___##__name = __loglevel; \
522 static const int *_loglevel___##__provider##___##__name = \
523 &_loglevel_value___##__provider##___##__name;
525 #include TRACEPOINT_INCLUDE
528 * Stage 7.1 of tracepoint event generation.
530 * Create events description structures. We use a weakref because
531 * loglevels are optional. If not declared, the event will point to the
532 * a loglevel that contains NULL.
535 /* Reset all macros within TRACEPOINT_EVENT */
536 #include <lttng/ust-tracepoint-event-reset.h>
538 #undef TRACEPOINT_EVENT_INSTANCE
539 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
541 __ref_loglevel___##_provider##___##_name \
542 __attribute__((weakref ("_loglevel___" #_provider "___" #_name))); \
543 const struct lttng_event_desc __event_desc___##_provider##_##_name = { \
544 .fields = __event_fields___##_provider##___##_template, \
545 .name = #_provider ":" #_name, \
546 .probe_callback = (void (*)(void)) &__event_probe__##_provider##___##_template,\
547 .nr_fields = _TP_ARRAY_SIZE(__event_fields___##_provider##___##_template), \
548 .loglevel = &__ref_loglevel___##_provider##___##_name, \
549 .signature = __tp_event_signature___##_provider##___##_template, \
552 #include TRACEPOINT_INCLUDE
555 * Stage 7.2 of tracepoint event generation.
557 * Create array of events.
560 /* Reset all macros within TRACEPOINT_EVENT */
561 #include <lttng/ust-tracepoint-event-reset.h>
563 #undef TRACEPOINT_EVENT_INSTANCE
564 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
565 &__event_desc___##_provider##_##_name,
567 static const struct lttng_event_desc
*_TP_COMBINE_TOKENS(__event_desc___
, TRACEPOINT_PROVIDER
)[] = {
568 #include TRACEPOINT_INCLUDE
573 * Stage 8 of tracepoint event generation.
575 * Create a toplevel descriptor for the whole probe.
578 /* non-const because list head will be modified when registered. */
579 static struct lttng_probe_desc
_TP_COMBINE_TOKENS(__probe_desc___
, TRACEPOINT_PROVIDER
) = {
580 .provider
= __tp_stringify(TRACEPOINT_PROVIDER
),
581 .event_desc
= _TP_COMBINE_TOKENS(__event_desc___
, TRACEPOINT_PROVIDER
),
582 .nr_events
= _TP_ARRAY_SIZE(_TP_COMBINE_TOKENS(__event_desc___
, TRACEPOINT_PROVIDER
)),
586 * Stage 9 of tracepoint event generation.
588 * Register/unregister probes at module load/unload.
590 * Generate the constructor as an externally visible symbol for use when
591 * linking the probe statically.
594 /* Reset all macros within TRACEPOINT_EVENT */
595 #include <lttng/ust-tracepoint-event-reset.h>
596 static void __attribute__((constructor
))
597 _TP_COMBINE_TOKENS(__lttng_events_init__
, TRACEPOINT_PROVIDER
)(void)
601 ret
= ltt_probe_register(&_TP_COMBINE_TOKENS(__probe_desc___
, TRACEPOINT_PROVIDER
));
605 static void __attribute__((destructor
))
606 _TP_COMBINE_TOKENS(__lttng_events_exit__
, TRACEPOINT_PROVIDER
)(void)
608 ltt_probe_unregister(&_TP_COMBINE_TOKENS(__probe_desc___
, TRACEPOINT_PROVIDER
));
611 int _TP_COMBINE_TOKENS(__tracepoint_provider_
, TRACEPOINT_PROVIDER
);