2 * Copyright (c) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
7 * Permission is hereby granted to use or copy this program
8 * for any purpose, provided the above notices are retained on all copies.
9 * Permission to modify the code and to distribute modified code is granted,
10 * provided the above notices are retained, and a notice that the code was
11 * modified is included with the above copyright notice.
15 #include <urcu/compiler.h>
16 #include <lttng/ust-events.h>
17 #include <lttng/ringbuffer-config.h>
21 * Macro declarations used for all stages.
25 #define ctf_integer(_type, _item, _src) \
26 ctf_integer_ext(_type, _item, _src, BYTE_ORDER, 10)
28 #undef ctf_integer_hex
29 #define ctf_integer_hex(_type, _item, _src) \
30 ctf_integer_ext(_type, _item, _src, BYTE_ORDER, 16)
32 #undef ctf_integer_network
33 #define ctf_integer_network(_type, _item, _src) \
34 ctf_integer_ext(_type, _item, _src, BIG_ENDIAN, 10)
36 #undef ctf_integer_network_hex
37 #define ctf_integer_network_hex(_type, _item, _src) \
38 ctf_integer_ext(_type, _item, _src, BIG_ENDIAN, 16)
40 /* ctf_float is redefined at each step */
43 #define ctf_array(_type, _item, _src, _length) \
44 ctf_array_encoded(_type, _item, _src, _length, none)
47 #define ctf_array_text(_type, _item, _src, _length) \
48 ctf_array_encoded(_type, _item, _src, _length, UTF8)
51 #define ctf_sequence(_type, _item, _src, _length_type, _src_length) \
52 ctf_sequence_encoded(_type, _item, _src, \
53 _length_type, _src_length, none)
55 #undef ctf_sequence_text
56 #define ctf_sequence_text(_type, _item, _src, _length_type, _src_length) \
57 ctf_sequence_encoded(_type, _item, _src, \
58 _length_type, _src_length, UTF8)
60 /* ctf_string is redefined at each step */
63 * TRACEPOINT_EVENT_CLASS declares a class of tracepoints receiving the
64 * same arguments and having the same field layout.
66 * TRACEPOINT_EVENT_INSTANCE declares an instance of a tracepoint, with
67 * its own provider and name. It refers to a class (template).
69 * TRACEPOINT_EVENT declared both a class and an instance and does a
70 * direct mapping from the instance to the class.
73 #undef TRACEPOINT_EVENT
74 #define TRACEPOINT_EVENT(_provider, _name, _args, _fields) \
75 TRACEPOINT_EVENT_CLASS(_provider, _name, \
77 _TP_PARAMS(_fields)) \
78 TRACEPOINT_EVENT_INSTANCE(_provider, _name, _name, \
82 #define _TP_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
84 #define _tp_max_t(type, x, y) \
88 __max1 > __max2 ? __max1: __max2; \
92 * Stage 0 of tracepoint event generation.
94 * Check that each TRACEPOINT_EVENT provider argument match the
95 * TRACEPOINT_PROVIDER by creating dummy callbacks.
98 /* Reset all macros within TRACEPOINT_EVENT */
99 #include <lttng/ust-tracepoint-event-reset.h>
102 void _TP_COMBINE_TOKENS(__tracepoint_provider_mismatch_
, TRACEPOINT_PROVIDER
)(void)
106 #undef TRACEPOINT_EVENT_CLASS
107 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
108 __tracepoint_provider_mismatch_##_provider();
110 #undef TRACEPOINT_EVENT_INSTANCE
111 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
112 __tracepoint_provider_mismatch_##_provider();
114 static __attribute__((unused
))
115 void _TP_COMBINE_TOKENS(__tracepoint_provider_check_
, TRACEPOINT_PROVIDER
)(void)
117 #include TRACEPOINT_INCLUDE
121 * Stage 0.1 of tracepoint event generation.
123 * Check that each TRACEPOINT_EVENT provider:name does not exceed the
124 * tracepoint name length limit.
127 /* Reset all macros within TRACEPOINT_EVENT */
128 #include <lttng/ust-tracepoint-event-reset.h>
130 #undef TRACEPOINT_EVENT_INSTANCE
131 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
133 __tp_name_len_check##_provider##___##_name[LTTNG_UST_SYM_NAME_LEN] \
134 __attribute__((unused)) = \
135 #_provider ":" #_name;
137 #include TRACEPOINT_INCLUDE
140 * Stage 1 of tracepoint event generation.
142 * Create event field type metadata section.
143 * Each event produce an array of fields.
146 /* Reset all macros within TRACEPOINT_EVENT */
147 #include <lttng/ust-tracepoint-event-reset.h>
149 #undef ctf_integer_ext
150 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
153 .type = __type_integer(_type, _byte_order, _base, none),\
157 #define ctf_float(_type, _item, _src) \
160 .type = __type_float(_type), \
163 #undef ctf_array_encoded
164 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
169 .atype = atype_array, \
173 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
178 #undef ctf_sequence_encoded
179 #define ctf_sequence_encoded(_type, _item, _src, \
180 _length_type, _src_length, _encoding) \
185 .atype = atype_sequence, \
188 .length_type = __type_integer(_length_type, BYTE_ORDER, 10, none), \
189 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
195 #define ctf_string(_item, _src) \
200 .atype = atype_string, \
201 .u.basic.string.encoding = lttng_encode_UTF8, \
206 #define TP_FIELDS(args...) args /* Only one used in this phase */
208 #undef TRACEPOINT_EVENT_CLASS
209 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
210 static const struct lttng_event_field __event_fields___##_provider##___##_name[] = { \
214 #include TRACEPOINT_INCLUDE
217 * Stage 2 of tracepoint event generation.
219 * Create probe callback prototypes.
222 /* Reset all macros within TRACEPOINT_EVENT */
223 #include <lttng/ust-tracepoint-event-reset.h>
226 #define TP_ARGS(args...) args
228 #undef TRACEPOINT_EVENT_CLASS
229 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
230 static void __event_probe__##_provider##___##_name(_TP_ARGS_DATA_PROTO(_args));
232 #include TRACEPOINT_INCLUDE
235 * Stage 3 of tracepoint event generation.
237 * Create static inline function that calculates event size.
240 /* Reset all macros within TRACEPOINT_EVENT */
241 #include <lttng/ust-tracepoint-event-reset.h>
243 #undef ctf_integer_ext
244 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
245 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
246 __event_len += sizeof(_type);
249 #define ctf_float(_type, _item, _src) \
250 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
251 __event_len += sizeof(_type);
253 #undef ctf_array_encoded
254 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
255 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
256 __event_len += sizeof(_type) * (_length);
258 #undef ctf_sequence_encoded
259 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
260 _src_length, _encoding) \
261 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_length_type)); \
262 __event_len += sizeof(_length_type); \
263 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
264 __dynamic_len[__dynamic_len_idx] = (_src_length); \
265 __event_len += sizeof(_type) * __dynamic_len[__dynamic_len_idx]; \
269 #define ctf_string(_item, _src) \
270 __event_len += __dynamic_len[__dynamic_len_idx++] = strlen(_src) + 1;
273 #define TP_ARGS(args...) args
276 #define TP_FIELDS(args...) args
278 #undef TRACEPOINT_EVENT_CLASS
279 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
280 static inline size_t __event_get_size__##_provider##___##_name(size_t *__dynamic_len, _TP_ARGS_DATA_PROTO(_args)) \
282 size_t __event_len = 0; \
283 unsigned int __dynamic_len_idx = 0; \
286 (void) __dynamic_len_idx; /* don't warn if unused */ \
288 return __event_len; \
291 #include TRACEPOINT_INCLUDE
294 * Stage 4 of tracepoint event generation.
296 * Create static inline function that calculates event payload alignment.
299 /* Reset all macros within TRACEPOINT_EVENT */
300 #include <lttng/ust-tracepoint-event-reset.h>
302 #undef ctf_integer_ext
303 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
304 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
307 #define ctf_float(_type, _item, _src) \
308 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
310 #undef ctf_array_encoded
311 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
312 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
314 #undef ctf_sequence_encoded
315 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
316 _src_length, _encoding) \
317 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_length_type)); \
318 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
321 #define ctf_string(_item, _src)
324 #define TP_ARGS(args...) args
327 #define TP_FIELDS(args...) args
329 #undef TRACEPOINT_EVENT_CLASS
330 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
332 size_t __event_get_align__##_provider##___##_name(_TP_ARGS_PROTO(_args)) \
334 size_t __event_align = 1; \
336 return __event_align; \
339 #include TRACEPOINT_INCLUDE
343 * Stage 5 of tracepoint event generation.
345 * Create the probe function. This function calls event size calculation
346 * and writes event data into the buffer.
349 /* Reset all macros within TRACEPOINT_EVENT */
350 #include <lttng/ust-tracepoint-event-reset.h>
352 #undef ctf_integer_ext
353 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
355 _type __tmp = (_src); \
356 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
357 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
361 #define ctf_float(_type, _item, _src) \
363 _type __tmp = (_src); \
364 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
365 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
368 #undef ctf_array_encoded
369 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
370 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
371 __chan->ops->event_write(&__ctx, _src, sizeof(_type) * (_length));
373 #undef ctf_sequence_encoded
374 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
375 _src_length, _encoding) \
377 _length_type __tmpl = __dynamic_len[__dynamic_len_idx]; \
378 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_length_type));\
379 __chan->ops->event_write(&__ctx, &__tmpl, sizeof(_length_type));\
381 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
382 __chan->ops->event_write(&__ctx, _src, \
383 sizeof(_type) * __get_dynamic_len(dest));
386 #define ctf_string(_item, _src) \
387 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(*(_src))); \
388 __chan->ops->event_write(&__ctx, _src, __get_dynamic_len(dest));
390 /* Beware: this get len actually consumes the len value */
391 #undef __get_dynamic_len
392 #define __get_dynamic_len(field) __dynamic_len[__dynamic_len_idx++]
395 #define TP_ARGS(args...) args
398 #define TP_FIELDS(args...) args
400 #undef TRACEPOINT_EVENT_CLASS
401 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
402 static void __event_probe__##_provider##___##_name(_TP_ARGS_DATA_PROTO(_args))\
404 struct ltt_event *__event = __tp_data; \
405 struct ltt_channel *__chan = __event->chan; \
406 struct lttng_ust_lib_ring_buffer_ctx __ctx; \
407 size_t __event_len, __event_align; \
408 size_t __dynamic_len_idx = 0; \
409 size_t __dynamic_len[_TP_ARRAY_SIZE(__event_fields___##_provider##___##_name)]; \
413 (void) __dynamic_len_idx; /* don't warn if unused */ \
414 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->session->active))) \
416 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->enabled))) \
418 if (caa_unlikely(!CMM_ACCESS_ONCE(__event->enabled))) \
420 __event_len = __event_get_size__##_provider##___##_name(__dynamic_len,\
421 _TP_ARGS_DATA_VAR(_args)); \
422 __event_align = __event_get_align__##_provider##___##_name(_TP_ARGS_VAR(_args)); \
423 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
424 __event_align, -1, __chan->handle); \
425 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
429 __chan->ops->event_commit(&__ctx); \
432 #include TRACEPOINT_INCLUDE
434 #undef __get_dynamic_len
437 * Stage 6 of tracepoint event generation.
439 * Tracepoint loglevel mapping definition generation. We generate a
440 * symbol for each mapping for a provider/event to ensure at most a 1 to
441 * 1 mapping between events and loglevels. If the symbol is repeated,
442 * the compiler will complain.
445 /* Reset all macros within TRACEPOINT_EVENT */
446 #include <lttng/ust-tracepoint-event-reset.h>
448 #undef TRACEPOINT_LOGLEVEL
449 #define TRACEPOINT_LOGLEVEL(__provider, __name, __loglevel) \
450 static const int _loglevel_value___##__provider##___##__name = __loglevel; \
451 static const int *_loglevel___##__provider##___##__name = \
452 &_loglevel_value___##__provider##___##__name;
454 #include TRACEPOINT_INCLUDE
457 * Stage 7.1 of tracepoint event generation.
459 * Create events description structures. We use a weakref because
460 * loglevels are optional. If not declared, the event will point to the
461 * a loglevel that contains NULL.
464 /* Reset all macros within TRACEPOINT_EVENT */
465 #include <lttng/ust-tracepoint-event-reset.h>
467 #undef TRACEPOINT_EVENT_INSTANCE
468 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
470 __ref_loglevel___##_provider##___##_name \
471 __attribute__((weakref ("_loglevel___" #_provider "___" #_name))); \
472 const struct lttng_event_desc __event_desc___##_provider##_##_name = { \
473 .fields = __event_fields___##_provider##___##_template, \
474 .name = #_provider ":" #_name, \
475 .probe_callback = (void *) &__event_probe__##_provider##___##_template,\
476 .nr_fields = _TP_ARRAY_SIZE(__event_fields___##_provider##___##_template), \
477 .loglevel = &__ref_loglevel___##_provider##___##_name, \
480 #include TRACEPOINT_INCLUDE
483 * Stage 7.2 of tracepoint event generation.
485 * Create array of events.
488 /* Reset all macros within TRACEPOINT_EVENT */
489 #include <lttng/ust-tracepoint-event-reset.h>
491 #undef TRACEPOINT_EVENT_INSTANCE
492 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
493 &__event_desc___##_provider##_##_name,
495 static const struct lttng_event_desc
*_TP_COMBINE_TOKENS(__event_desc___
, TRACEPOINT_PROVIDER
)[] = {
496 #include TRACEPOINT_INCLUDE
501 * Stage 8 of tracepoint event generation.
503 * Create a toplevel descriptor for the whole probe.
506 /* non-const because list head will be modified when registered. */
507 static struct lttng_probe_desc
_TP_COMBINE_TOKENS(__probe_desc___
, TRACEPOINT_PROVIDER
) = {
508 .provider
= __tp_stringify(TRACEPOINT_PROVIDER
),
509 .event_desc
= _TP_COMBINE_TOKENS(__event_desc___
, TRACEPOINT_PROVIDER
),
510 .nr_events
= _TP_ARRAY_SIZE(_TP_COMBINE_TOKENS(__event_desc___
, TRACEPOINT_PROVIDER
)),
514 * Stage 9 of tracepoint event generation.
516 * Register/unregister probes at module load/unload.
518 * Generate the constructor as an externally visible symbol for use when
519 * linking the probe statically.
522 /* Reset all macros within TRACEPOINT_EVENT */
523 #include <lttng/ust-tracepoint-event-reset.h>
524 static void __attribute__((constructor
))
525 _TP_COMBINE_TOKENS(__lttng_events_init__
, TRACEPOINT_PROVIDER
)(void)
529 ret
= ltt_probe_register(&_TP_COMBINE_TOKENS(__probe_desc___
, TRACEPOINT_PROVIDER
));
533 static void __attribute__((destructor
))
534 _TP_COMBINE_TOKENS(__lttng_events_exit__
, TRACEPOINT_PROVIDER
)(void)
536 ltt_probe_unregister(&_TP_COMBINE_TOKENS(__probe_desc___
, TRACEPOINT_PROVIDER
));
539 int _TP_COMBINE_TOKENS(__tracepoint_provider_
, TRACEPOINT_PROVIDER
);