2 * Copyright (C) 2009 Steven Rostedt <srostedt@redhat.com>
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation;
8 * version 2.1 of the License.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <urcu/compiler.h>
22 #include <lttng/ust-events.h>
23 #include <lttng/usterr-signal-safe.h>
24 #include <lttng/ringbuffer-config.h>
27 * Macro declarations used for all stages.
31 #define ctf_integer(_type, _item, _src) \
32 ctf_integer_ext(_type, _item, _src, BYTE_ORDER, 10)
34 #undef ctf_integer_hex
35 #define ctf_integer_hex(_type, _item, _src) \
36 ctf_integer_ext(_type, _item, _src, BYTE_ORDER, 16)
38 #undef ctf_integer_network
39 #define ctf_integer_network(_type, _item, _src) \
40 ctf_integer_ext(_type, _item, _src, BIG_ENDIAN, 10)
42 #undef ctf_integer_network_hex
43 #define ctf_integer_network_hex(_type, _item, _src) \
44 ctf_integer_ext(_type, _item, _src, BIG_ENDIAN, 16)
46 /* ctf_float is redefined at each step */
49 #define ctf_array(_type, _item, _src, _length) \
50 ctf_array_encoded(_type, _item, _src, _length, none)
53 #define ctf_array_text(_type, _item, _src, _length) \
54 ctf_array_encoded(_type, _item, _src, _length, UTF8)
57 #define ctf_sequence(_type, _item, _src, _length_type, _src_length) \
58 ctf_sequence_encoded(_type, _item, _src, \
59 _length_type, _src_length, none)
61 #undef ctf_sequence_text
62 #define ctf_sequence_text(_type, _item, _src, _length_type, _src_length) \
63 ctf_sequence_encoded(_type, _item, _src, \
64 _length_type, _src_length, UTF8)
66 /* ctf_string is redefined at each step */
69 * TRACEPOINT_EVENT_CLASS can be used to add a generic function handlers
70 * for events. That is, if all events have the same parameters and just
71 * have distinct trace points. Each tracepoint can be defined with
72 * TRACEPOINT_EVENT_INSTANCE and that will map the
73 * TRACEPOINT_EVENT_CLASS to the tracepoint.
75 * TRACEPOINT_EVENT is a one to one mapping between tracepoint and
79 #undef TRACEPOINT_EVENT
80 #define TRACEPOINT_EVENT(provider, name, args, fields) \
81 TRACEPOINT_EVENT_CLASS(provider, name, \
84 TRACEPOINT_EVENT_INSTANCE(provider, name, name, TP_PARAMS(args))
87 #define _TP_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
89 #define _tp_max_t(type, x, y) \
93 __max1 > __max2 ? __max1: __max2; \
97 * Stage 0 of the trace events.
99 * Check that each TRACEPOINT_EVENT provider argument match the
100 * TRACEPOINT_PROVIDER by creating dummy callbacks.
103 /* Reset all macros within TRACEPOINT_EVENT */
104 #include <lttng/ust-tracepoint-event-reset.h>
106 #define TP_ID1(_token, _provider) _token##_provider
107 #define TP_ID(_token, _provider) TP_ID1(_token, _provider)
110 void TP_ID(__tracepoint_provider_mismatch_
, TRACEPOINT_PROVIDER
)(void)
114 #undef TRACEPOINT_EVENT_CLASS
115 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
116 __tracepoint_provider_mismatch_##_provider();
118 #undef TRACEPOINT_EVENT_INSTANCE
119 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
120 __tracepoint_provider_mismatch_##_provider();
122 static __attribute__((unused
))
123 void TP_ID(__tracepoint_provider_check_
, TRACEPOINT_PROVIDER
)(void)
125 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
132 * Stage 1 of the trace events.
134 * Create event field type metadata section.
135 * Each event produce an array of fields.
138 /* Reset all macros within TRACEPOINT_EVENT */
139 #include <lttng/ust-tracepoint-event-reset.h>
141 #undef ctf_integer_ext
142 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
145 .type = __type_integer(_type, _byte_order, _base, none),\
149 #define ctf_float(_type, _item, _src) \
152 .type = __type_float(_type), \
155 #undef ctf_array_encoded
156 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
161 .atype = atype_array, \
165 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
170 #undef ctf_sequence_encoded
171 #define ctf_sequence_encoded(_type, _item, _src, \
172 _length_type, _src_length, _encoding) \
177 .atype = atype_sequence, \
180 .length_type = __type_integer(_length_type, BYTE_ORDER, 10, none), \
181 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
187 #define ctf_string(_item, _src) \
192 .atype = atype_string, \
193 .u.basic.string.encoding = lttng_encode_UTF8, \
198 #define TP_FIELDS(args...) args /* Only one used in this phase */
200 #undef TRACEPOINT_EVENT_CLASS
201 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
202 static const struct lttng_event_field __event_fields___##_provider##___##_name[] = { \
206 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
209 * Stage 2 of the trace events.
211 * Create probe callback prototypes.
214 /* Reset all macros within TRACEPOINT_EVENT */
215 #include <lttng/ust-tracepoint-event-reset.h>
218 #define TP_ARGS(args...) args
220 #undef TRACEPOINT_EVENT_CLASS
221 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
222 static void __event_probe__##_provider##___##_name(_TP_ARGS_PROTO_DATA(_args));
224 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
227 * Stage 3 of the trace events.
229 * Create an array of events.
232 /* Reset all macros within TRACEPOINT_EVENT */
233 #include <lttng/ust-tracepoint-event-reset.h>
235 #undef TRACEPOINT_EVENT_INSTANCE
236 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
238 .fields = __event_fields___##_provider##___##_template,\
239 .name = #_provider ":" #_name, \
240 .probe_callback = (void *) &__event_probe__##_provider##___##_template,\
241 .nr_fields = _TP_ARRAY_SIZE(__event_fields___##_provider##___##_template), \
244 #define TP_ID1(_token, _provider) _token##_provider
245 #define TP_ID(_token, _provider) TP_ID1(_token, _provider)
247 static const struct lttng_event_desc
TP_ID(__event_desc___
, TRACEPOINT_PROVIDER
)[] = {
248 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
256 * Stage 4 of the trace events.
258 * Create a toplevel descriptor for the whole probe.
261 #define TP_ID1(_token, _provider) _token##_provider
262 #define TP_ID(_token, _provider) TP_ID1(_token, _provider)
264 /* non-const because list head will be modified when registered. */
265 static struct lttng_probe_desc
TP_ID(__probe_desc___
, TRACEPOINT_PROVIDER
) = {
266 .event_desc
= TP_ID(__event_desc___
, TRACEPOINT_PROVIDER
),
267 .nr_events
= _TP_ARRAY_SIZE(TP_ID(__event_desc___
, TRACEPOINT_PROVIDER
)),
274 * Stage 5 of the trace events.
276 * Create static inline function that calculates event size.
279 /* Reset all macros within TRACEPOINT_EVENT */
280 #include <lttng/ust-tracepoint-event-reset.h>
282 #undef ctf_integer_ext
283 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
284 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
285 __event_len += sizeof(_type);
288 #define ctf_float(_type, _item, _src) \
289 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
290 __event_len += sizeof(_type);
292 #undef ctf_array_encoded
293 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
294 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
295 __event_len += sizeof(_type) * (_length);
297 #undef ctf_sequence_encoded
298 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
299 _src_length, _encoding) \
300 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_length_type)); \
301 __event_len += sizeof(_length_type); \
302 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
303 __dynamic_len[__dynamic_len_idx] = (_src_length); \
304 __event_len += sizeof(_type) * __dynamic_len[__dynamic_len_idx]; \
308 #define ctf_string(_item, _src) \
309 __event_len += __dynamic_len[__dynamic_len_idx++] = strlen(_src) + 1;
312 #define TP_ARGS(args...) args
315 #define TP_FIELDS(args...) args
317 #undef TRACEPOINT_EVENT_CLASS
318 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
319 static inline size_t __event_get_size__##_provider##___##_name(size_t *__dynamic_len, _TP_ARGS_PROTO_DATA(_args)) \
321 size_t __event_len = 0; \
322 unsigned int __dynamic_len_idx = 0; \
325 (void) __dynamic_len_idx; /* don't warn if unused */ \
327 return __event_len; \
330 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
333 * Stage 6 of the trace events.
335 * Create static inline function that calculates event payload alignment.
338 /* Reset all macros within TRACEPOINT_EVENT */
339 #include <lttng/ust-tracepoint-event-reset.h>
341 #undef ctf_integer_ext
342 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
343 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
346 #define ctf_float(_type, _item, _src) \
347 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
349 #undef ctf_array_encoded
350 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
351 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
353 #undef ctf_sequence_encoded
354 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
355 _src_length, _encoding) \
356 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_length_type)); \
357 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
360 #define ctf_string(_item, _src)
363 #define TP_ARGS(args...) args
366 #define TP_FIELDS(args...) args
368 #undef TRACEPOINT_EVENT_CLASS
369 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
371 size_t __event_get_align__##_provider##___##_name(_TP_ARGS_PROTO(_args)) \
373 size_t __event_align = 1; \
375 return __event_align; \
378 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
382 * Stage 7 of the trace events.
384 * Create the probe function : call even size calculation and write event data
387 * We use both the field and assignment macros to write the fields in the order
388 * defined in the field declaration. The field declarations control the
389 * execution order, jumping to the appropriate assignment block.
392 /* Reset all macros within TRACEPOINT_EVENT */
393 #include <lttng/ust-tracepoint-event-reset.h>
395 #undef ctf_integer_ext
396 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
398 _type __tmp = (_src); \
399 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
400 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
404 #define ctf_float(_type, _item, _src) \
406 _type __tmp = (_src); \
407 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
408 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
411 #undef ctf_array_encoded
412 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
413 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
414 __chan->ops->event_write(&__ctx, _src, sizeof(_type) * (_length));
416 #undef ctf_sequence_encoded
417 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
418 _src_length, _encoding) \
420 _length_type __tmpl = __dynamic_len[__dynamic_len_idx]; \
421 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_length_type));\
422 __chan->ops->event_write(&__ctx, &__tmpl, sizeof(_length_type));\
424 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
425 __chan->ops->event_write(&__ctx, _src, \
426 sizeof(_type) * __get_dynamic_len(dest));
429 #define ctf_string(_item, _src) \
430 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(*(_src))); \
431 __chan->ops->event_write(&__ctx, _src, __get_dynamic_len(dest));
433 /* Beware: this get len actually consumes the len value */
434 #undef __get_dynamic_len
435 #define __get_dynamic_len(field) __dynamic_len[__dynamic_len_idx++]
438 #define TP_ARGS(args...) args
441 #define TP_FIELDS(args...) args
443 #undef TRACEPOINT_EVENT_CLASS
444 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
445 static void __event_probe__##_provider##___##_name(_TP_ARGS_PROTO_DATA(_args))\
447 struct ltt_event *__event = __tp_cb_data; \
448 struct ltt_channel *__chan = __event->chan; \
449 struct lttng_ust_lib_ring_buffer_ctx __ctx; \
450 size_t __event_len, __event_align; \
451 size_t __dynamic_len_idx = 0; \
452 size_t __dynamic_len[_TP_ARRAY_SIZE(__event_fields___##_provider##___##_name)]; \
456 (void) __dynamic_len_idx; /* don't warn if unused */ \
457 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->session->active))) \
459 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->enabled))) \
461 if (caa_unlikely(!CMM_ACCESS_ONCE(__event->enabled))) \
463 __event_len = __event_get_size__##_provider##___##_name(__dynamic_len,\
464 _TP_ARGS_VARS_DATA(_args)); \
465 __event_align = __event_get_align__##_provider##___##_name(_TP_ARGS_VARS(_args)); \
466 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
467 __event_align, -1, __chan->handle); \
468 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
472 __chan->ops->event_commit(&__ctx); \
475 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
477 #undef __get_dynamic_len
480 * Stage 8 of the trace events.
482 * Register/unregister probes at module load/unload.
485 /* Reset all macros within TRACEPOINT_EVENT */
486 #include <lttng/ust-tracepoint-event-reset.h>
488 #define TP_ID1(_token, _provider) _token##_provider
489 #define TP_ID(_token, _provider) TP_ID1(_token, _provider)
491 static void __attribute__((constructor
))
492 TP_ID(__lttng_events_init__
, TRACEPOINT_PROVIDER
)(void)
496 ret
= ltt_probe_register(&TP_ID(__probe_desc___
, TRACEPOINT_PROVIDER
));
500 static void __attribute__((destructor
))
501 TP_ID(__lttng_events_exit__
, TRACEPOINT_PROVIDER
)(void)
503 ltt_probe_unregister(&TP_ID(__probe_desc___
, TRACEPOINT_PROVIDER
));