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/usterr-signal-safe.h>
18 #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 1 of tracepoint event generation.
123 * Create event field type metadata section.
124 * Each event produce an array of fields.
127 /* Reset all macros within TRACEPOINT_EVENT */
128 #include <lttng/ust-tracepoint-event-reset.h>
130 #undef ctf_integer_ext
131 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
134 .type = __type_integer(_type, _byte_order, _base, none),\
138 #define ctf_float(_type, _item, _src) \
141 .type = __type_float(_type), \
144 #undef ctf_array_encoded
145 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
150 .atype = atype_array, \
154 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
159 #undef ctf_sequence_encoded
160 #define ctf_sequence_encoded(_type, _item, _src, \
161 _length_type, _src_length, _encoding) \
166 .atype = atype_sequence, \
169 .length_type = __type_integer(_length_type, BYTE_ORDER, 10, none), \
170 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
176 #define ctf_string(_item, _src) \
181 .atype = atype_string, \
182 .u.basic.string.encoding = lttng_encode_UTF8, \
187 #define TP_FIELDS(args...) args /* Only one used in this phase */
189 #undef TRACEPOINT_EVENT_CLASS
190 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
191 static const struct lttng_event_field __event_fields___##_provider##___##_name[] = { \
195 #include TRACEPOINT_INCLUDE
198 * Stage 2 of tracepoint event generation.
200 * Create probe callback prototypes.
203 /* Reset all macros within TRACEPOINT_EVENT */
204 #include <lttng/ust-tracepoint-event-reset.h>
207 #define TP_ARGS(args...) args
209 #undef TRACEPOINT_EVENT_CLASS
210 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
211 static void __event_probe__##_provider##___##_name(_TP_ARGS_DATA_PROTO(_args));
213 #include TRACEPOINT_INCLUDE
216 * Stage 3 of tracepoint event generation.
218 * Create an array of events.
221 /* Reset all macros within TRACEPOINT_EVENT */
222 #include <lttng/ust-tracepoint-event-reset.h>
224 #undef TRACEPOINT_EVENT_INSTANCE
225 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
227 .fields = __event_fields___##_provider##___##_template,\
228 .name = #_provider ":" #_name, \
229 .probe_callback = (void *) &__event_probe__##_provider##___##_template,\
230 .nr_fields = _TP_ARRAY_SIZE(__event_fields___##_provider##___##_template), \
233 static const struct lttng_event_desc
_TP_COMBINE_TOKENS(__event_desc___
, TRACEPOINT_PROVIDER
)[] = {
234 #include TRACEPOINT_INCLUDE
239 * Stage 4 of tracepoint event generation.
241 * Create a toplevel descriptor for the whole probe.
244 /* non-const because list head will be modified when registered. */
245 static struct lttng_probe_desc
_TP_COMBINE_TOKENS(__probe_desc___
, TRACEPOINT_PROVIDER
) = {
246 .event_desc
= _TP_COMBINE_TOKENS(__event_desc___
, TRACEPOINT_PROVIDER
),
247 .nr_events
= _TP_ARRAY_SIZE(_TP_COMBINE_TOKENS(__event_desc___
, TRACEPOINT_PROVIDER
)),
251 * Stage 5 of tracepoint event generation.
253 * Create static inline function that calculates event size.
256 /* Reset all macros within TRACEPOINT_EVENT */
257 #include <lttng/ust-tracepoint-event-reset.h>
259 #undef ctf_integer_ext
260 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
261 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
262 __event_len += sizeof(_type);
265 #define ctf_float(_type, _item, _src) \
266 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
267 __event_len += sizeof(_type);
269 #undef ctf_array_encoded
270 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
271 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
272 __event_len += sizeof(_type) * (_length);
274 #undef ctf_sequence_encoded
275 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
276 _src_length, _encoding) \
277 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_length_type)); \
278 __event_len += sizeof(_length_type); \
279 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
280 __dynamic_len[__dynamic_len_idx] = (_src_length); \
281 __event_len += sizeof(_type) * __dynamic_len[__dynamic_len_idx]; \
285 #define ctf_string(_item, _src) \
286 __event_len += __dynamic_len[__dynamic_len_idx++] = strlen(_src) + 1;
289 #define TP_ARGS(args...) args
292 #define TP_FIELDS(args...) args
294 #undef TRACEPOINT_EVENT_CLASS
295 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
296 static inline size_t __event_get_size__##_provider##___##_name(size_t *__dynamic_len, _TP_ARGS_DATA_PROTO(_args)) \
298 size_t __event_len = 0; \
299 unsigned int __dynamic_len_idx = 0; \
302 (void) __dynamic_len_idx; /* don't warn if unused */ \
304 return __event_len; \
307 #include TRACEPOINT_INCLUDE
310 * Stage 6 of tracepoint event generation.
312 * Create static inline function that calculates event payload alignment.
315 /* Reset all macros within TRACEPOINT_EVENT */
316 #include <lttng/ust-tracepoint-event-reset.h>
318 #undef ctf_integer_ext
319 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
320 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
323 #define ctf_float(_type, _item, _src) \
324 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
326 #undef ctf_array_encoded
327 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
328 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
330 #undef ctf_sequence_encoded
331 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
332 _src_length, _encoding) \
333 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_length_type)); \
334 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
337 #define ctf_string(_item, _src)
340 #define TP_ARGS(args...) args
343 #define TP_FIELDS(args...) args
345 #undef TRACEPOINT_EVENT_CLASS
346 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
348 size_t __event_get_align__##_provider##___##_name(_TP_ARGS_PROTO(_args)) \
350 size_t __event_align = 1; \
352 return __event_align; \
355 #include TRACEPOINT_INCLUDE
359 * Stage 7 of tracepoint event generation.
361 * Create the probe function. This function calls event size calculation
362 * and writes event data into the buffer.
365 /* Reset all macros within TRACEPOINT_EVENT */
366 #include <lttng/ust-tracepoint-event-reset.h>
368 #undef ctf_integer_ext
369 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
371 _type __tmp = (_src); \
372 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
373 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
377 #define ctf_float(_type, _item, _src) \
379 _type __tmp = (_src); \
380 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
381 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
384 #undef ctf_array_encoded
385 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
386 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
387 __chan->ops->event_write(&__ctx, _src, sizeof(_type) * (_length));
389 #undef ctf_sequence_encoded
390 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
391 _src_length, _encoding) \
393 _length_type __tmpl = __dynamic_len[__dynamic_len_idx]; \
394 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_length_type));\
395 __chan->ops->event_write(&__ctx, &__tmpl, sizeof(_length_type));\
397 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
398 __chan->ops->event_write(&__ctx, _src, \
399 sizeof(_type) * __get_dynamic_len(dest));
402 #define ctf_string(_item, _src) \
403 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(*(_src))); \
404 __chan->ops->event_write(&__ctx, _src, __get_dynamic_len(dest));
406 /* Beware: this get len actually consumes the len value */
407 #undef __get_dynamic_len
408 #define __get_dynamic_len(field) __dynamic_len[__dynamic_len_idx++]
411 #define TP_ARGS(args...) args
414 #define TP_FIELDS(args...) args
416 #undef TRACEPOINT_EVENT_CLASS
417 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
418 static void __event_probe__##_provider##___##_name(_TP_ARGS_DATA_PROTO(_args))\
420 struct ltt_event *__event = __tp_cb_data; \
421 struct ltt_channel *__chan = __event->chan; \
422 struct lttng_ust_lib_ring_buffer_ctx __ctx; \
423 size_t __event_len, __event_align; \
424 size_t __dynamic_len_idx = 0; \
425 size_t __dynamic_len[_TP_ARRAY_SIZE(__event_fields___##_provider##___##_name)]; \
429 (void) __dynamic_len_idx; /* don't warn if unused */ \
430 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->session->active))) \
432 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->enabled))) \
434 if (caa_unlikely(!CMM_ACCESS_ONCE(__event->enabled))) \
436 __event_len = __event_get_size__##_provider##___##_name(__dynamic_len,\
437 _TP_ARGS_DATA_VAR(_args)); \
438 __event_align = __event_get_align__##_provider##___##_name(_TP_ARGS_VAR(_args)); \
439 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
440 __event_align, -1, __chan->handle); \
441 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
445 __chan->ops->event_commit(&__ctx); \
448 #include TRACEPOINT_INCLUDE
450 #undef __get_dynamic_len
453 * Stage 8 of tracepoint event generation.
455 * Register/unregister probes at module load/unload.
458 /* Reset all macros within TRACEPOINT_EVENT */
459 #include <lttng/ust-tracepoint-event-reset.h>
461 static void __attribute__((constructor
))
462 _TP_COMBINE_TOKENS(__lttng_events_init__
, TRACEPOINT_PROVIDER
)(void)
466 ret
= ltt_probe_register(&_TP_COMBINE_TOKENS(__probe_desc___
, TRACEPOINT_PROVIDER
));
470 static void __attribute__((destructor
))
471 _TP_COMBINE_TOKENS(__lttng_events_exit__
, TRACEPOINT_PROVIDER
)(void)
473 ltt_probe_unregister(&_TP_COMBINE_TOKENS(__probe_desc___
, TRACEPOINT_PROVIDER
));