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, proto, args, fields) \
81 TRACEPOINT_EVENT_CLASS(provider, name, \
85 TRACEPOINT_EVENT_INSTANCE(provider, name, name, TP_PARAMS(proto), TP_PARAMS(args))
87 #undef TRACEPOINT_EVENT_NOARGS
88 #define TRACEPOINT_EVENT_NOARGS(provider, name, fields) \
89 TRACEPOINT_EVENT_CLASS_NOARGS(provider, name, \
91 TRACEPOINT_EVENT_INSTANCE_NOARGS(provider, name, name)
94 #define _TP_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
96 #define _tp_max_t(type, x, y) \
100 __max1 > __max2 ? __max1: __max2; \
104 * Stage 0 of the trace events.
106 * Check that each TRACEPOINT_EVENT provider argument match the
107 * TRACEPOINT_PROVIDER by creating dummy callbacks.
110 /* Reset all macros within TRACEPOINT_EVENT */
111 #include <lttng/ust-tracepoint-event-reset.h>
113 #define TP_ID1(_token, _provider) _token##_provider
114 #define TP_ID(_token, _provider) TP_ID1(_token, _provider)
117 void TP_ID(__tracepoint_provider_mismatch_
, TRACEPOINT_PROVIDER
)(void)
121 #undef TRACEPOINT_EVENT_CLASS
122 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _proto, _args, _fields) \
123 __tracepoint_provider_mismatch_##_provider();
125 #undef TRACEPOINT_EVENT_CLASS_NOARGS
126 #define TRACEPOINT_EVENT_CLASS_NOARGS(_provider, _name, _fields) \
127 __tracepoint_provider_mismatch_##_provider();
129 #undef TRACEPOINT_EVENT_INSTANCE
130 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _proto, _args) \
131 __tracepoint_provider_mismatch_##_provider();
133 #undef TRACEPOINT_EVENT_INSTANCE_NOARGS
134 #define TRACEPOINT_EVENT_INSTANCE_NOARGS(_provider, _template, _name) \
135 __tracepoint_provider_mismatch_##_provider();
137 static __attribute__((unused
))
138 void TP_ID(__tracepoint_provider_check_
, TRACEPOINT_PROVIDER
)(void)
140 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
147 * Stage 1 of the trace events.
149 * Create event field type metadata section.
150 * Each event produce an array of fields.
153 /* Reset all macros within TRACEPOINT_EVENT */
154 #include <lttng/ust-tracepoint-event-reset.h>
156 #undef ctf_integer_ext
157 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
160 .type = __type_integer(_type, _byte_order, _base, none),\
164 #define ctf_float(_type, _item, _src) \
167 .type = __type_float(_type), \
170 #undef ctf_array_encoded
171 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
176 .atype = atype_array, \
180 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
185 #undef ctf_sequence_encoded
186 #define ctf_sequence_encoded(_type, _item, _src, \
187 _length_type, _src_length, _encoding) \
192 .atype = atype_sequence, \
195 .length_type = __type_integer(_length_type, BYTE_ORDER, 10, none), \
196 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
202 #define ctf_string(_item, _src) \
207 .atype = atype_string, \
208 .u.basic.string.encoding = lttng_encode_UTF8, \
213 #define TP_FIELDS(args...) args /* Only one used in this phase */
215 #undef TRACEPOINT_EVENT_CLASS_NOARGS
216 #define TRACEPOINT_EVENT_CLASS_NOARGS(_provider, _name, _fields) \
217 static const struct lttng_event_field __event_fields___##_provider##___##_name[] = { \
221 #undef TRACEPOINT_EVENT_CLASS
222 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _proto, _args, _fields) \
223 TRACEPOINT_EVENT_CLASS_NOARGS(_provider, _name, TP_PARAMS(_fields))
225 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
228 * Stage 2 of the trace events.
230 * Create probe callback prototypes.
233 /* Reset all macros within TRACEPOINT_EVENT */
234 #include <lttng/ust-tracepoint-event-reset.h>
237 #define TP_PROTO(args...) args
239 #undef TRACEPOINT_EVENT_CLASS
240 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _proto, _args, _fields) \
241 static void __event_probe__##_provider##___##_name(void *__data, _proto);
243 #undef TRACEPOINT_EVENT_CLASS_NOARGS
244 #define TRACEPOINT_EVENT_CLASS_NOARGS(_provider, _name, _fields) \
245 static void __event_probe__##_provider##___##_name(void *__data);
247 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
250 * Stage 3 of the trace events.
252 * Create an array of events.
255 /* Reset all macros within TRACEPOINT_EVENT */
256 #include <lttng/ust-tracepoint-event-reset.h>
258 #undef TRACEPOINT_EVENT_INSTANCE_NOARGS
259 #define TRACEPOINT_EVENT_INSTANCE_NOARGS(_provider, _template, _name) \
261 .fields = __event_fields___##_provider##___##_template,\
262 .name = #_provider ":" #_name, \
263 .probe_callback = (void *) &__event_probe__##_provider##___##_template,\
264 .nr_fields = _TP_ARRAY_SIZE(__event_fields___##_provider##___##_template), \
267 #undef TRACEPOINT_EVENT_INSTANCE
268 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _proto, _args) \
269 TRACEPOINT_EVENT_INSTANCE_NOARGS(_provider, _template, _name)
271 #define TP_ID1(_token, _provider) _token##_provider
272 #define TP_ID(_token, _provider) TP_ID1(_token, _provider)
274 static const struct lttng_event_desc
TP_ID(__event_desc___
, TRACEPOINT_PROVIDER
)[] = {
275 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
283 * Stage 4 of the trace events.
285 * Create a toplevel descriptor for the whole probe.
288 #define TP_ID1(_token, _provider) _token##_provider
289 #define TP_ID(_token, _provider) TP_ID1(_token, _provider)
291 /* non-const because list head will be modified when registered. */
292 static struct lttng_probe_desc
TP_ID(__probe_desc___
, TRACEPOINT_PROVIDER
) = {
293 .event_desc
= TP_ID(__event_desc___
, TRACEPOINT_PROVIDER
),
294 .nr_events
= _TP_ARRAY_SIZE(TP_ID(__event_desc___
, TRACEPOINT_PROVIDER
)),
301 * Stage 5 of the trace events.
303 * Create static inline function that calculates event size.
306 /* Reset all macros within TRACEPOINT_EVENT */
307 #include <lttng/ust-tracepoint-event-reset.h>
309 #undef ctf_integer_ext
310 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
311 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
312 __event_len += sizeof(_type);
315 #define ctf_float(_type, _item, _src) \
316 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
317 __event_len += sizeof(_type);
319 #undef ctf_array_encoded
320 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
321 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
322 __event_len += sizeof(_type) * (_length);
324 #undef ctf_sequence_encoded
325 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
326 _src_length, _encoding) \
327 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_length_type)); \
328 __event_len += sizeof(_length_type); \
329 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
330 __dynamic_len[__dynamic_len_idx] = (_src_length); \
331 __event_len += sizeof(_type) * __dynamic_len[__dynamic_len_idx]; \
335 #define ctf_string(_item, _src) \
336 __event_len += __dynamic_len[__dynamic_len_idx++] = strlen(_src) + 1;
339 #define TP_PROTO(args...) args
342 #define TP_FIELDS(args...) args
344 #undef TRACEPOINT_EVENT_CLASS
345 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _proto, _args, _fields) \
346 static inline size_t __event_get_size__##_provider##___##_name(size_t *__dynamic_len, _proto) \
348 size_t __event_len = 0; \
349 unsigned int __dynamic_len_idx = 0; \
352 (void) __dynamic_len_idx; /* don't warn if unused */ \
354 return __event_len; \
357 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
360 * Stage 6 of the trace events.
362 * Create static inline function that calculates event payload alignment.
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) \
370 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
373 #define ctf_float(_type, _item, _src) \
374 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
376 #undef ctf_array_encoded
377 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
378 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
380 #undef ctf_sequence_encoded
381 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
382 _src_length, _encoding) \
383 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_length_type)); \
384 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
387 #define ctf_string(_item, _src)
390 #define TP_PROTO(args...) args
393 #define TP_FIELDS(args...) args
395 #undef TRACEPOINT_EVENT_CLASS
396 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _proto, _args, _fields) \
397 static inline size_t __event_get_align__##_provider##___##_name(_proto) \
399 size_t __event_align = 1; \
401 return __event_align; \
404 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
408 * Stage 7 of the trace events.
410 * Create the probe function : call even size calculation and write event data
413 * We use both the field and assignment macros to write the fields in the order
414 * defined in the field declaration. The field declarations control the
415 * execution order, jumping to the appropriate assignment block.
418 /* Reset all macros within TRACEPOINT_EVENT */
419 #include <lttng/ust-tracepoint-event-reset.h>
421 #undef ctf_integer_ext
422 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
424 _type __tmp = (_src); \
425 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
426 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
430 #define ctf_float(_type, _item, _src) \
432 _type __tmp = (_src); \
433 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
434 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
437 #undef ctf_array_encoded
438 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
439 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
440 __chan->ops->event_write(&__ctx, _src, sizeof(_type) * (_length));
442 #undef ctf_sequence_encoded
443 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
444 _src_length, _encoding) \
446 _length_type __tmpl = __dynamic_len[__dynamic_len_idx]; \
447 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_length_type));\
448 __chan->ops->event_write(&__ctx, &__tmpl, sizeof(_length_type));\
450 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
451 __chan->ops->event_write(&__ctx, _src, \
452 sizeof(_type) * __get_dynamic_len(dest));
455 #define ctf_string(_item, _src) \
456 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(*(_src))); \
457 __chan->ops->event_write(&__ctx, _src, __get_dynamic_len(dest));
459 /* Beware: this get len actually consumes the len value */
460 #undef __get_dynamic_len
461 #define __get_dynamic_len(field) __dynamic_len[__dynamic_len_idx++]
464 #define TP_PROTO(args...) args
467 #define TP_VARS(args...) args
470 #define TP_FIELDS(args...) args
472 #undef TRACEPOINT_EVENT_CLASS
473 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _proto, _args, _fields) \
474 static void __event_probe__##_provider##___##_name(void *__data, _proto) \
476 struct ltt_event *__event = __data; \
477 struct ltt_channel *__chan = __event->chan; \
478 struct lttng_ust_lib_ring_buffer_ctx __ctx; \
479 size_t __event_len, __event_align; \
480 size_t __dynamic_len_idx = 0; \
481 size_t __dynamic_len[_TP_ARRAY_SIZE(__event_fields___##_provider##___##_name)]; \
485 (void) __dynamic_len_idx; /* don't warn if unused */ \
486 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->session->active))) \
488 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->enabled))) \
490 if (caa_unlikely(!CMM_ACCESS_ONCE(__event->enabled))) \
492 __event_len = __event_get_size__##_provider##___##_name(__dynamic_len, _args); \
493 __event_align = __event_get_align__##_provider##___##_name(_args); \
494 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
495 __event_align, -1, __chan->handle); \
496 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
500 __chan->ops->event_commit(&__ctx); \
503 #undef TRACEPOINT_EVENT_CLASS_NOARGS
504 #define TRACEPOINT_EVENT_CLASS_NOARGS(_provider, _name, _fields) \
505 static void __event_probe__##_provider##___##_name(void *__data) \
507 struct ltt_event *__event = __data; \
508 struct ltt_channel *__chan = __event->chan; \
509 struct lttng_ust_lib_ring_buffer_ctx __ctx; \
510 size_t __event_len, __event_align; \
513 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->session->active))) \
515 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->enabled))) \
517 if (caa_unlikely(!CMM_ACCESS_ONCE(__event->enabled))) \
521 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
522 __event_align, -1, __chan->handle); \
523 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
527 __chan->ops->event_commit(&__ctx); \
530 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
532 #undef __get_dynamic_len
535 * Stage 8 of the trace events.
537 * Register/unregister probes at module load/unload.
540 /* Reset all macros within TRACEPOINT_EVENT */
541 #include <lttng/ust-tracepoint-event-reset.h>
543 #define TP_ID1(_token, _provider) _token##_provider
544 #define TP_ID(_token, _provider) TP_ID1(_token, _provider)
546 static void __attribute__((constructor
))
547 TP_ID(__lttng_events_init__
, TRACEPOINT_PROVIDER
)(void)
551 ret
= ltt_probe_register(&TP_ID(__probe_desc___
, TRACEPOINT_PROVIDER
));
555 static void __attribute__((destructor
))
556 TP_ID(__lttng_events_exit__
, TRACEPOINT_PROVIDER
)(void)
558 ltt_probe_unregister(&TP_ID(__probe_desc___
, TRACEPOINT_PROVIDER
));