492423acce0ea5786cab65fb7818775050945f21
4 * Copyright (C) 2009 Steven Rostedt <rostedt@goodmis.org>
5 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; only
10 * version 2.1 of the License.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <linux/uaccess.h>
22 #include <linux/debugfs.h>
24 #include "lttng-types.h"
25 #include "../wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
26 #include "../wrapper/ringbuffer/frontend_types.h"
27 #include "../lttng-events.h"
28 #include "../lttng-tracer-core.h"
31 * Macro declarations used for all stages.
35 * DECLARE_EVENT_CLASS can be used to add a generic function
36 * handlers for events. That is, if all events have the same
37 * parameters and just have distinct trace points.
38 * Each tracepoint can be defined with DEFINE_EVENT and that
39 * will map the DECLARE_EVENT_CLASS to the tracepoint.
41 * TRACE_EVENT is a one to one mapping between tracepoint and template.
45 #define TRACE_EVENT(name, proto, args, tstruct, assign, print) \
46 DECLARE_EVENT_CLASS(name, \
52 DEFINE_EVENT(name, name, PARAMS(proto), PARAMS(args))
54 #undef TRACE_EVENT_NOARGS
55 #define TRACE_EVENT_NOARGS(name, tstruct, assign, print) \
56 DECLARE_EVENT_CLASS_NOARGS(name, \
60 DEFINE_EVENT_NOARGS(name, name)
63 #undef DEFINE_EVENT_PRINT
64 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
65 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
67 /* Callbacks are meaningless to LTTng. */
69 #define TRACE_EVENT_FN(name, proto, args, tstruct, \
70 assign, print, reg, unreg) \
71 TRACE_EVENT(name, PARAMS(proto), PARAMS(args), \
72 PARAMS(tstruct), PARAMS(assign), PARAMS(print)) \
75 * Stage 1 of the trace events.
77 * Create dummy trace calls for each events, verifying that the LTTng module
78 * TRACE_EVENT headers match the kernel arguments. Will be optimized out by the
82 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
85 #define TP_PROTO(args...) args
88 #define TP_ARGS(args...) args
91 #define DEFINE_EVENT(_template, _name, _proto, _args) \
92 void trace_##_name(_proto);
94 #undef DEFINE_EVENT_NOARGS
95 #define DEFINE_EVENT_NOARGS(_template, _name) \
96 void trace_##_name(void *__data);
98 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
101 * Stage 2 of the trace events.
103 * Create event field type metadata section.
104 * Each event produce an array of fields.
107 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
109 /* Named field types must be defined in lttng-types.h */
112 #define __field_full(_type, _item, _order, _base) \
115 .type = __type_integer(_type, _order, _base, none), \
119 #define __field(_type, _item) \
120 __field_full(_type, _item, __BYTE_ORDER, 10)
123 #define __field_ext(_type, _item, _filter_type) \
124 __field(_type, _item)
127 #define __field_hex(_type, _item) \
128 __field_full(_type, _item, __BYTE_ORDER, 16)
130 #undef __field_network
131 #define __field_network(_type, _item) \
132 __field_full(_type, _item, __BIG_ENDIAN, 10)
134 #undef __field_network_hex
135 #define __field_network_hex(_type, _item) \
136 __field_full(_type, _item, __BIG_ENDIAN, 16)
138 #undef __array_enc_ext
139 #define __array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
144 .atype = atype_array, \
148 .elem_type = __type_integer(_type, _order, _base, _encoding), \
154 #define __array(_type, _item, _length) \
155 __array_enc_ext(_type, _item, _length, __BYTE_ORDER, 10, none)
158 #define __array_text(_type, _item, _length) \
159 __array_enc_ext(_type, _item, _length, __BYTE_ORDER, 10, UTF8)
162 #define __array_hex(_type, _item, _length) \
163 __array_enc_ext(_type, _item, _length, __BYTE_ORDER, 16, none)
165 #undef __dynamic_array_enc_ext
166 #define __dynamic_array_enc_ext(_type, _item, _length, _order, _base, _encoding) \
171 .atype = atype_sequence, \
174 .length_type = __type_integer(u32, __BYTE_ORDER, 10, none), \
175 .elem_type = __type_integer(_type, _order, _base, _encoding), \
180 #undef __dynamic_array
181 #define __dynamic_array(_type, _item, _length) \
182 __dynamic_array_enc_ext(_type, _item, _length, __BYTE_ORDER, 10, none)
184 #undef __dynamic_array_text
185 #define __dynamic_array_text(_type, _item, _length) \
186 __dynamic_array_enc_ext(_type, _item, _length, __BYTE_ORDER, 10, UTF8)
188 #undef __dynamic_array_hex
189 #define __dynamic_array_hex(_type, _item, _length) \
190 __dynamic_array_enc_ext(_type, _item, _length, __BYTE_ORDER, 16, none)
193 #define __string(_item, _src) \
198 .atype = atype_string, \
199 .u.basic.string.encoding = lttng_encode_UTF8, \
203 #undef __string_from_user
204 #define __string_from_user(_item, _src) \
205 __string(_item, _src)
207 #undef TP_STRUCT__entry
208 #define TP_STRUCT__entry(args...) args /* Only one used in this phase */
210 #undef DECLARE_EVENT_CLASS_NOARGS
211 #define DECLARE_EVENT_CLASS_NOARGS(_name, _tstruct, _assign, _print) \
212 static const struct lttng_event_field __event_fields___##_name[] = { \
216 #undef DECLARE_EVENT_CLASS
217 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
218 DECLARE_EVENT_CLASS_NOARGS(_name, PARAMS(_tstruct), PARAMS(_assign), \
221 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
224 * Stage 3 of the trace events.
226 * Create probe callback prototypes.
229 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
232 #define TP_PROTO(args...) args
234 #undef DECLARE_EVENT_CLASS
235 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
236 static void __event_probe__##_name(void *__data, _proto);
238 #undef DECLARE_EVENT_CLASS_NOARGS
239 #define DECLARE_EVENT_CLASS_NOARGS(_name, _tstruct, _assign, _print) \
240 static void __event_probe__##_name(void *__data);
242 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
245 * Stage 3.9 of the trace events.
247 * Create event descriptions.
250 /* Named field types must be defined in lttng-types.h */
252 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
255 #define TP_PROBE_CB(_template) &__event_probe__##_template
258 #undef DEFINE_EVENT_NOARGS
259 #define DEFINE_EVENT_NOARGS(_template, _name) \
260 static const struct lttng_event_desc __event_desc___##_name = { \
261 .fields = __event_fields___##_template, \
263 .probe_callback = (void *) TP_PROBE_CB(_template), \
264 .nr_fields = ARRAY_SIZE(__event_fields___##_template), \
265 .owner = THIS_MODULE, \
269 #define DEFINE_EVENT(_template, _name, _proto, _args) \
270 DEFINE_EVENT_NOARGS(_template, _name)
272 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
276 * Stage 4 of the trace events.
278 * Create an array of event description pointers.
281 /* Named field types must be defined in lttng-types.h */
283 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
285 #undef DEFINE_EVENT_NOARGS
286 #define DEFINE_EVENT_NOARGS(_template, _name) \
287 &__event_desc___##_name,
290 #define DEFINE_EVENT(_template, _name, _proto, _args) \
291 DEFINE_EVENT_NOARGS(_template, _name)
293 #define TP_ID1(_token, _system) _token##_system
294 #define TP_ID(_token, _system) TP_ID1(_token, _system)
296 static const struct lttng_event_desc
*TP_ID(__event_desc___
, TRACE_SYSTEM
)[] = {
297 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
305 * Stage 5 of the trace events.
307 * Create a toplevel descriptor for the whole probe.
310 #define TP_ID1(_token, _system) _token##_system
311 #define TP_ID(_token, _system) TP_ID1(_token, _system)
313 /* non-const because list head will be modified when registered. */
314 static __used
struct lttng_probe_desc
TP_ID(__probe_desc___
, TRACE_SYSTEM
) = {
315 .event_desc
= TP_ID(__event_desc___
, TRACE_SYSTEM
),
316 .nr_events
= ARRAY_SIZE(TP_ID(__event_desc___
, TRACE_SYSTEM
)),
323 * Stage 6 of the trace events.
325 * Create static inline function that calculates event size.
328 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
330 /* Named field types must be defined in lttng-types.h */
333 #define __field_full(_type, _item, _order, _base) \
334 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
335 __event_len += sizeof(_type);
337 #undef __array_enc_ext
338 #define __array_enc_ext(_type, _item, _length, _order, _base, _encoding) \
339 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
340 __event_len += sizeof(_type) * (_length);
342 #undef __dynamic_array_enc_ext
343 #define __dynamic_array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
344 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(u32)); \
345 __event_len += sizeof(u32); \
346 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
347 __dynamic_len[__dynamic_len_idx] = (_length); \
348 __event_len += sizeof(_type) * __dynamic_len[__dynamic_len_idx]; \
352 #define __string(_item, _src) \
353 __event_len += __dynamic_len[__dynamic_len_idx++] = strlen(_src) + 1;
356 * strlen_user includes \0. If returns 0, it faulted, so we set size to
359 #undef __string_from_user
360 #define __string_from_user(_item, _src) \
361 __event_len += __dynamic_len[__dynamic_len_idx++] = \
362 max_t(size_t, strlen_user(_src), 1);
365 #define TP_PROTO(args...) args
367 #undef TP_STRUCT__entry
368 #define TP_STRUCT__entry(args...) args
370 #undef DECLARE_EVENT_CLASS
371 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
372 static inline size_t __event_get_size__##_name(size_t *__dynamic_len, _proto) \
374 size_t __event_len = 0; \
375 unsigned int __dynamic_len_idx = 0; \
378 (void) __dynamic_len_idx; /* don't warn if unused */ \
380 return __event_len; \
383 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
386 * Stage 7 of the trace events.
388 * Create static inline function that calculates event payload alignment.
391 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
393 /* Named field types must be defined in lttng-types.h */
396 #define __field_full(_type, _item, _order, _base) \
397 __event_align = max_t(size_t, __event_align, lttng_alignof(_type));
399 #undef __array_enc_ext
400 #define __array_enc_ext(_type, _item, _length, _order, _base, _encoding) \
401 __event_align = max_t(size_t, __event_align, lttng_alignof(_type));
403 #undef __dynamic_array_enc_ext
404 #define __dynamic_array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
405 __event_align = max_t(size_t, __event_align, lttng_alignof(u32)); \
406 __event_align = max_t(size_t, __event_align, lttng_alignof(_type));
409 #define __string(_item, _src)
411 #undef __string_from_user
412 #define __string_from_user(_item, _src)
415 #define TP_PROTO(args...) args
417 #undef TP_STRUCT__entry
418 #define TP_STRUCT__entry(args...) args
420 #undef DECLARE_EVENT_CLASS
421 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
422 static inline size_t __event_get_align__##_name(_proto) \
424 size_t __event_align = 1; \
426 return __event_align; \
429 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
433 * Stage 8 of the trace events.
435 * Create structure declaration that allows the "assign" macros to access the
439 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
441 /* Named field types must be defined in lttng-types.h */
444 #define __field_full(_type, _item, _order, _base) _type _item;
446 #undef __array_enc_ext
447 #define __array_enc_ext(_type, _item, _length, _order, _base, _encoding) \
450 #undef __dynamic_array_enc_ext
451 #define __dynamic_array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
455 #define __string(_item, _src) char _item;
457 #undef __string_from_user
458 #define __string_from_user(_item, _src) \
459 __string(_item, _src)
461 #undef TP_STRUCT__entry
462 #define TP_STRUCT__entry(args...) args
464 #undef DECLARE_EVENT_CLASS
465 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
466 struct __event_typemap__##_name { \
470 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
474 * Stage 9 of the trace events.
476 * Create the probe function : call even size calculation and write event data
479 * We use both the field and assignment macros to write the fields in the order
480 * defined in the field declaration. The field declarations control the
481 * execution order, jumping to the appropriate assignment block.
484 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
487 #define __field_full(_type, _item, _order, _base) \
488 goto __assign_##_item; \
491 #undef __array_enc_ext
492 #define __array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
493 goto __assign_##_item; \
496 #undef __dynamic_array_enc_ext
497 #define __dynamic_array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
498 goto __assign_##_item##_1; \
499 __end_field_##_item##_1: \
500 goto __assign_##_item##_2; \
501 __end_field_##_item##_2:
504 #define __string(_item, _src) \
505 goto __assign_##_item; \
508 #undef __string_from_user
509 #define __string_from_user(_item, _src) \
510 __string(_item, _src)
513 * Macros mapping tp_assign() to "=", tp_memcpy() to memcpy() and tp_strcpy() to
517 #define tp_assign(dest, src) \
520 __typeof__(__typemap.dest) __tmp = (src); \
521 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp)); \
522 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
524 goto __end_field_##dest;
526 /* fixed length array memcpy */
528 #define tp_memcpy_gen(write_ops, dest, src, len) \
531 (void) __typemap.dest; \
532 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__typemap.dest)); \
533 __chan->ops->write_ops(&__ctx, src, len); \
534 goto __end_field_##dest;
537 #define tp_memcpy(dest, src, len) \
538 tp_memcpy_gen(event_write, dest, src, len)
540 #undef tp_memcpy_from_user
541 #define tp_memcpy_from_user(dest, src, len) \
542 tp_memcpy_gen(event_write_from_user, dest, src, len)
544 /* variable length sequence memcpy */
545 #undef tp_memcpy_dyn_gen
546 #define tp_memcpy_dyn_gen(write_ops, dest, src) \
547 __assign_##dest##_1: \
549 u32 __tmpl = __dynamic_len[__dynamic_len_idx]; \
550 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(u32)); \
551 __chan->ops->event_write(&__ctx, &__tmpl, sizeof(u32)); \
553 goto __end_field_##dest##_1; \
554 __assign_##dest##_2: \
555 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__typemap.dest)); \
556 __chan->ops->write_ops(&__ctx, src, \
557 sizeof(__typemap.dest) * __get_dynamic_array_len(dest));\
558 goto __end_field_##dest##_2;
561 #define tp_memcpy_dyn(dest, src) \
562 tp_memcpy_dyn_gen(event_write, dest, src)
564 #undef tp_memcpy_dyn_from_user
565 #define tp_memcpy_dyn_from_user(dest, src) \
566 tp_memcpy_dyn_gen(event_write_from_user, dest, src)
569 * The string length including the final \0.
571 #undef tp_copy_string_from_user
572 #define tp_copy_string_from_user(dest, src) \
578 (void) __typemap.dest; \
579 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__typemap.dest));\
580 __ustrlen = __get_dynamic_array_len(dest); \
581 if (likely(__ustrlen > 1)) { \
582 __chan->ops->event_write_from_user(&__ctx, src, \
585 __chan->ops->event_memset(&__ctx, 0, 1); \
587 goto __end_field_##dest;
589 #define tp_strcpy(dest, src) \
590 tp_memcpy(dest, src, __get_dynamic_array_len(dest))
592 /* Named field types must be defined in lttng-types.h */
595 #define __get_str(field) field
597 #undef __get_dynamic_array
598 #define __get_dynamic_array(field) field
600 /* Beware: this get len actually consumes the len value */
601 #undef __get_dynamic_array_len
602 #define __get_dynamic_array_len(field) __dynamic_len[__dynamic_len_idx++]
605 #define TP_PROTO(args...) args
608 #define TP_ARGS(args...) args
610 #undef TP_STRUCT__entry
611 #define TP_STRUCT__entry(args...) args
613 #undef TP_fast_assign
614 #define TP_fast_assign(args...) args
617 * For state dump, check that "session" argument (mandatory) matches the
618 * session this event belongs to. Ensures that we write state dump data only
619 * into the started session, not into all sessions.
621 #ifdef TP_SESSION_CHECK
622 #define _TP_SESSION_CHECK(session, csession) (session == csession)
623 #else /* TP_SESSION_CHECK */
624 #define _TP_SESSION_CHECK(session, csession) 1
625 #endif /* TP_SESSION_CHECK */
627 #undef DECLARE_EVENT_CLASS
628 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
629 static void __event_probe__##_name(void *__data, _proto) \
631 struct lttng_event *__event = __data; \
632 struct lttng_channel *__chan = __event->chan; \
633 struct lib_ring_buffer_ctx __ctx; \
634 size_t __event_len, __event_align; \
635 size_t __dynamic_len_idx = 0; \
636 size_t __dynamic_len[ARRAY_SIZE(__event_fields___##_name)]; \
637 struct __event_typemap__##_name __typemap; \
641 (void) __dynamic_len_idx; /* don't warn if unused */ \
642 (void) __typemap; /* don't warn if unused */ \
644 if (!_TP_SESSION_CHECK(session, __chan->session)) \
646 if (unlikely(!ACCESS_ONCE(__chan->session->active))) \
648 if (unlikely(!ACCESS_ONCE(__chan->enabled))) \
650 if (unlikely(!ACCESS_ONCE(__event->enabled))) \
652 __event_len = __event_get_size__##_name(__dynamic_len, _args); \
653 __event_align = __event_get_align__##_name(_args); \
654 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
655 __event_align, -1); \
656 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
659 /* Control code (field ordering) */ \
661 __chan->ops->event_commit(&__ctx); \
663 /* Copy code, steered by control code */ \
667 #undef DECLARE_EVENT_CLASS_NOARGS
668 #define DECLARE_EVENT_CLASS_NOARGS(_name, _tstruct, _assign, _print) \
669 static void __event_probe__##_name(void *__data) \
671 struct lttng_event *__event = __data; \
672 struct lttng_channel *__chan = __event->chan; \
673 struct lib_ring_buffer_ctx __ctx; \
674 size_t __event_len, __event_align; \
677 if (!_TP_SESSION_CHECK(session, __chan->session)) \
679 if (unlikely(!ACCESS_ONCE(__chan->session->active))) \
681 if (unlikely(!ACCESS_ONCE(__chan->enabled))) \
683 if (unlikely(!ACCESS_ONCE(__event->enabled))) \
687 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
688 __event_align, -1); \
689 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
692 /* Control code (field ordering) */ \
694 __chan->ops->event_commit(&__ctx); \
696 /* Copy code, steered by control code */ \
700 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
703 * Stage 10 of the trace events.
705 * Register/unregister probes at module load/unload.
708 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
710 #define TP_ID1(_token, _system) _token##_system
711 #define TP_ID(_token, _system) TP_ID1(_token, _system)
712 #define module_init_eval1(_token, _system) module_init(_token##_system)
713 #define module_init_eval(_token, _system) module_init_eval1(_token, _system)
714 #define module_exit_eval1(_token, _system) module_exit(_token##_system)
715 #define module_exit_eval(_token, _system) module_exit_eval1(_token, _system)
717 #ifndef TP_MODULE_OVERRIDE
718 static int TP_ID(__lttng_events_init__
, TRACE_SYSTEM
)(void)
720 wrapper_vmalloc_sync_all();
721 return lttng_probe_register(&TP_ID(__probe_desc___
, TRACE_SYSTEM
));
724 module_init_eval(__lttng_events_init__
, TRACE_SYSTEM
);
726 static void TP_ID(__lttng_events_exit__
, TRACE_SYSTEM
)(void)
728 lttng_probe_unregister(&TP_ID(__probe_desc___
, TRACE_SYSTEM
));
731 module_exit_eval(__lttng_events_exit__
, TRACE_SYSTEM
);
734 #undef module_init_eval
735 #undef module_exit_eval
741 #undef TRACE_EVENT_FLAGS
This page took 0.044438 seconds and 4 git commands to generate.