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 "lttng-probe-user.h"
26 #include "../wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
27 #include "../wrapper/ringbuffer/frontend_types.h"
28 #include "../lttng-events.h"
29 #include "../lttng-tracer-core.h"
32 * Macro declarations used for all stages.
36 * DECLARE_EVENT_CLASS can be used to add a generic function
37 * handlers for events. That is, if all events have the same
38 * parameters and just have distinct trace points.
39 * Each tracepoint can be defined with DEFINE_EVENT and that
40 * will map the DECLARE_EVENT_CLASS to the tracepoint.
42 * TRACE_EVENT is a one to one mapping between tracepoint and template.
46 #define TRACE_EVENT(name, proto, args, tstruct, assign, print) \
47 DECLARE_EVENT_CLASS(name, \
53 DEFINE_EVENT(name, name, PARAMS(proto), PARAMS(args))
55 #undef TRACE_EVENT_NOARGS
56 #define TRACE_EVENT_NOARGS(name, tstruct, assign, print) \
57 DECLARE_EVENT_CLASS_NOARGS(name, \
61 DEFINE_EVENT_NOARGS(name, name)
64 #undef DEFINE_EVENT_PRINT
65 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
66 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
68 /* Callbacks are meaningless to LTTng. */
70 #define TRACE_EVENT_FN(name, proto, args, tstruct, \
71 assign, print, reg, unreg) \
72 TRACE_EVENT(name, PARAMS(proto), PARAMS(args), \
73 PARAMS(tstruct), PARAMS(assign), PARAMS(print)) \
76 * Stage 1 of the trace events.
78 * Create dummy trace calls for each events, verifying that the LTTng module
79 * TRACE_EVENT headers match the kernel arguments. Will be optimized out by the
83 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
86 #define TP_PROTO(args...) args
89 #define TP_ARGS(args...) args
92 #define DEFINE_EVENT(_template, _name, _proto, _args) \
93 void trace_##_name(_proto);
95 #undef DEFINE_EVENT_NOARGS
96 #define DEFINE_EVENT_NOARGS(_template, _name) \
97 void trace_##_name(void *__data);
99 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
102 * Stage 2 of the trace events.
104 * Create event field type metadata section.
105 * Each event produce an array of fields.
108 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
110 /* Named field types must be defined in lttng-types.h */
113 #define __field_full(_type, _item, _order, _base) \
116 .type = __type_integer(_type, _order, _base, none), \
120 #define __field(_type, _item) \
121 __field_full(_type, _item, __BYTE_ORDER, 10)
124 #define __field_ext(_type, _item, _filter_type) \
125 __field(_type, _item)
128 #define __field_hex(_type, _item) \
129 __field_full(_type, _item, __BYTE_ORDER, 16)
131 #undef __field_network
132 #define __field_network(_type, _item) \
133 __field_full(_type, _item, __BIG_ENDIAN, 10)
135 #undef __field_network_hex
136 #define __field_network_hex(_type, _item) \
137 __field_full(_type, _item, __BIG_ENDIAN, 16)
139 #undef __array_enc_ext
140 #define __array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
145 .atype = atype_array, \
149 .elem_type = __type_integer(_type, _order, _base, _encoding), \
155 #define __array(_type, _item, _length) \
156 __array_enc_ext(_type, _item, _length, __BYTE_ORDER, 10, none)
159 #define __array_text(_type, _item, _length) \
160 __array_enc_ext(_type, _item, _length, __BYTE_ORDER, 10, UTF8)
163 #define __array_hex(_type, _item, _length) \
164 __array_enc_ext(_type, _item, _length, __BYTE_ORDER, 16, none)
166 #undef __dynamic_array_enc_ext
167 #define __dynamic_array_enc_ext(_type, _item, _length, _order, _base, _encoding) \
172 .atype = atype_sequence, \
175 .length_type = __type_integer(u32, __BYTE_ORDER, 10, none), \
176 .elem_type = __type_integer(_type, _order, _base, _encoding), \
181 #undef __dynamic_array
182 #define __dynamic_array(_type, _item, _length) \
183 __dynamic_array_enc_ext(_type, _item, _length, __BYTE_ORDER, 10, none)
185 #undef __dynamic_array_text
186 #define __dynamic_array_text(_type, _item, _length) \
187 __dynamic_array_enc_ext(_type, _item, _length, __BYTE_ORDER, 10, UTF8)
189 #undef __dynamic_array_hex
190 #define __dynamic_array_hex(_type, _item, _length) \
191 __dynamic_array_enc_ext(_type, _item, _length, __BYTE_ORDER, 16, none)
194 #define __string(_item, _src) \
199 .atype = atype_string, \
200 .u.basic.string.encoding = lttng_encode_UTF8, \
204 #undef __string_from_user
205 #define __string_from_user(_item, _src) \
206 __string(_item, _src)
208 #undef TP_STRUCT__entry
209 #define TP_STRUCT__entry(args...) args /* Only one used in this phase */
211 #undef DECLARE_EVENT_CLASS_NOARGS
212 #define DECLARE_EVENT_CLASS_NOARGS(_name, _tstruct, _assign, _print) \
213 static const struct lttng_event_field __event_fields___##_name[] = { \
217 #undef DECLARE_EVENT_CLASS
218 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
219 DECLARE_EVENT_CLASS_NOARGS(_name, PARAMS(_tstruct), PARAMS(_assign), \
222 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
225 * Stage 3 of the trace events.
227 * Create probe callback prototypes.
230 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
233 #define TP_PROTO(args...) args
235 #undef DECLARE_EVENT_CLASS
236 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
237 static void __event_probe__##_name(void *__data, _proto);
239 #undef DECLARE_EVENT_CLASS_NOARGS
240 #define DECLARE_EVENT_CLASS_NOARGS(_name, _tstruct, _assign, _print) \
241 static void __event_probe__##_name(void *__data);
243 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
246 * Stage 3.9 of the trace events.
248 * Create event descriptions.
251 /* Named field types must be defined in lttng-types.h */
253 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
256 #define TP_PROBE_CB(_template) &__event_probe__##_template
259 #undef DEFINE_EVENT_NOARGS
260 #define DEFINE_EVENT_NOARGS(_template, _name) \
261 static const struct lttng_event_desc __event_desc___##_name = { \
262 .fields = __event_fields___##_template, \
264 .probe_callback = (void *) TP_PROBE_CB(_template), \
265 .nr_fields = ARRAY_SIZE(__event_fields___##_template), \
266 .owner = THIS_MODULE, \
270 #define DEFINE_EVENT(_template, _name, _proto, _args) \
271 DEFINE_EVENT_NOARGS(_template, _name)
273 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
277 * Stage 4 of the trace events.
279 * Create an array of event description pointers.
282 /* Named field types must be defined in lttng-types.h */
284 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
286 #undef DEFINE_EVENT_NOARGS
287 #define DEFINE_EVENT_NOARGS(_template, _name) \
288 &__event_desc___##_name,
291 #define DEFINE_EVENT(_template, _name, _proto, _args) \
292 DEFINE_EVENT_NOARGS(_template, _name)
294 #define TP_ID1(_token, _system) _token##_system
295 #define TP_ID(_token, _system) TP_ID1(_token, _system)
297 static const struct lttng_event_desc
*TP_ID(__event_desc___
, TRACE_SYSTEM
)[] = {
298 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
306 * Stage 5 of the trace events.
308 * Create a toplevel descriptor for the whole probe.
311 #define TP_ID1(_token, _system) _token##_system
312 #define TP_ID(_token, _system) TP_ID1(_token, _system)
314 /* non-const because list head will be modified when registered. */
315 static __used
struct lttng_probe_desc
TP_ID(__probe_desc___
, TRACE_SYSTEM
) = {
316 .event_desc
= TP_ID(__event_desc___
, TRACE_SYSTEM
),
317 .nr_events
= ARRAY_SIZE(TP_ID(__event_desc___
, TRACE_SYSTEM
)),
324 * Stage 6 of the trace events.
326 * Create static inline function that calculates event size.
329 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
331 /* Named field types must be defined in lttng-types.h */
334 #define __field_full(_type, _item, _order, _base) \
335 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
336 __event_len += sizeof(_type);
338 #undef __array_enc_ext
339 #define __array_enc_ext(_type, _item, _length, _order, _base, _encoding) \
340 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
341 __event_len += sizeof(_type) * (_length);
343 #undef __dynamic_array_enc_ext
344 #define __dynamic_array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
345 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(u32)); \
346 __event_len += sizeof(u32); \
347 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
348 __dynamic_len[__dynamic_len_idx] = (_length); \
349 __event_len += sizeof(_type) * __dynamic_len[__dynamic_len_idx]; \
353 #define __string(_item, _src) \
354 __event_len += __dynamic_len[__dynamic_len_idx++] = strlen(_src) + 1;
357 * strlen_user includes \0. If returns 0, it faulted, so we set size to
360 #undef __string_from_user
361 #define __string_from_user(_item, _src) \
362 __event_len += __dynamic_len[__dynamic_len_idx++] = \
363 max_t(size_t, lttng_strlen_user_inatomic(_src), 1);
366 #define TP_PROTO(args...) args
368 #undef TP_STRUCT__entry
369 #define TP_STRUCT__entry(args...) args
371 #undef DECLARE_EVENT_CLASS
372 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
373 static inline size_t __event_get_size__##_name(size_t *__dynamic_len, _proto) \
375 size_t __event_len = 0; \
376 unsigned int __dynamic_len_idx = 0; \
379 (void) __dynamic_len_idx; /* don't warn if unused */ \
381 return __event_len; \
384 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
387 * Stage 7 of the trace events.
389 * Create static inline function that calculates event payload alignment.
392 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
394 /* Named field types must be defined in lttng-types.h */
397 #define __field_full(_type, _item, _order, _base) \
398 __event_align = max_t(size_t, __event_align, lttng_alignof(_type));
400 #undef __array_enc_ext
401 #define __array_enc_ext(_type, _item, _length, _order, _base, _encoding) \
402 __event_align = max_t(size_t, __event_align, lttng_alignof(_type));
404 #undef __dynamic_array_enc_ext
405 #define __dynamic_array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
406 __event_align = max_t(size_t, __event_align, lttng_alignof(u32)); \
407 __event_align = max_t(size_t, __event_align, lttng_alignof(_type));
410 #define __string(_item, _src)
412 #undef __string_from_user
413 #define __string_from_user(_item, _src)
416 #define TP_PROTO(args...) args
418 #undef TP_STRUCT__entry
419 #define TP_STRUCT__entry(args...) args
421 #undef DECLARE_EVENT_CLASS
422 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
423 static inline size_t __event_get_align__##_name(_proto) \
425 size_t __event_align = 1; \
427 return __event_align; \
430 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
434 * Stage 8 of the trace events.
436 * Create structure declaration that allows the "assign" macros to access the
440 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
442 /* Named field types must be defined in lttng-types.h */
445 #define __field_full(_type, _item, _order, _base) _type _item;
447 #undef __array_enc_ext
448 #define __array_enc_ext(_type, _item, _length, _order, _base, _encoding) \
451 #undef __dynamic_array_enc_ext
452 #define __dynamic_array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
456 #define __string(_item, _src) char _item;
458 #undef __string_from_user
459 #define __string_from_user(_item, _src) \
460 __string(_item, _src)
462 #undef TP_STRUCT__entry
463 #define TP_STRUCT__entry(args...) args
465 #undef DECLARE_EVENT_CLASS
466 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
467 struct __event_typemap__##_name { \
471 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
475 * Stage 9 of the trace events.
477 * Create the probe function : call even size calculation and write event data
480 * We use both the field and assignment macros to write the fields in the order
481 * defined in the field declaration. The field declarations control the
482 * execution order, jumping to the appropriate assignment block.
485 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
488 #define __field_full(_type, _item, _order, _base) \
489 goto __assign_##_item; \
492 #undef __array_enc_ext
493 #define __array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
494 goto __assign_##_item; \
497 #undef __dynamic_array_enc_ext
498 #define __dynamic_array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
499 goto __assign_##_item##_1; \
500 __end_field_##_item##_1: \
501 goto __assign_##_item##_2; \
502 __end_field_##_item##_2:
505 #define __string(_item, _src) \
506 goto __assign_##_item; \
509 #undef __string_from_user
510 #define __string_from_user(_item, _src) \
511 __string(_item, _src)
514 * Macros mapping tp_assign() to "=", tp_memcpy() to memcpy() and tp_strcpy() to
518 #define tp_assign(dest, src) \
521 __typeof__(__typemap.dest) __tmp = (src); \
522 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp)); \
523 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
525 goto __end_field_##dest;
527 /* fixed length array memcpy */
529 #define tp_memcpy_gen(write_ops, dest, src, len) \
532 (void) __typemap.dest; \
533 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__typemap.dest)); \
534 __chan->ops->write_ops(&__ctx, src, len); \
535 goto __end_field_##dest;
538 #define tp_memcpy(dest, src, len) \
539 tp_memcpy_gen(event_write, dest, src, len)
541 #undef tp_memcpy_from_user
542 #define tp_memcpy_from_user(dest, src, len) \
543 tp_memcpy_gen(event_write_from_user, dest, src, len)
545 /* variable length sequence memcpy */
546 #undef tp_memcpy_dyn_gen
547 #define tp_memcpy_dyn_gen(write_ops, dest, src) \
548 __assign_##dest##_1: \
550 u32 __tmpl = __dynamic_len[__dynamic_len_idx]; \
551 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(u32)); \
552 __chan->ops->event_write(&__ctx, &__tmpl, sizeof(u32)); \
554 goto __end_field_##dest##_1; \
555 __assign_##dest##_2: \
556 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__typemap.dest)); \
557 __chan->ops->write_ops(&__ctx, src, \
558 sizeof(__typemap.dest) * __get_dynamic_array_len(dest));\
559 goto __end_field_##dest##_2;
562 #define tp_memcpy_dyn(dest, src) \
563 tp_memcpy_dyn_gen(event_write, dest, src)
565 #undef tp_memcpy_dyn_from_user
566 #define tp_memcpy_dyn_from_user(dest, src) \
567 tp_memcpy_dyn_gen(event_write_from_user, dest, src)
570 * The string length including the final \0.
572 #undef tp_copy_string_from_user
573 #define tp_copy_string_from_user(dest, src) \
579 (void) __typemap.dest; \
580 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__typemap.dest));\
581 __ustrlen = __get_dynamic_array_len(dest); \
582 if (likely(__ustrlen > 1)) { \
583 __chan->ops->event_write_from_user(&__ctx, src, \
586 __chan->ops->event_memset(&__ctx, 0, 1); \
588 goto __end_field_##dest;
590 #define tp_strcpy(dest, src) \
591 tp_memcpy(dest, src, __get_dynamic_array_len(dest))
593 /* Named field types must be defined in lttng-types.h */
596 #define __get_str(field) field
598 #undef __get_dynamic_array
599 #define __get_dynamic_array(field) field
601 /* Beware: this get len actually consumes the len value */
602 #undef __get_dynamic_array_len
603 #define __get_dynamic_array_len(field) __dynamic_len[__dynamic_len_idx++]
606 #define TP_PROTO(args...) args
609 #define TP_ARGS(args...) args
611 #undef TP_STRUCT__entry
612 #define TP_STRUCT__entry(args...) args
614 #undef TP_fast_assign
615 #define TP_fast_assign(args...) args
618 * For state dump, check that "session" argument (mandatory) matches the
619 * session this event belongs to. Ensures that we write state dump data only
620 * into the started session, not into all sessions.
622 #ifdef TP_SESSION_CHECK
623 #define _TP_SESSION_CHECK(session, csession) (session == csession)
624 #else /* TP_SESSION_CHECK */
625 #define _TP_SESSION_CHECK(session, csession) 1
626 #endif /* TP_SESSION_CHECK */
628 #undef DECLARE_EVENT_CLASS
629 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
630 static void __event_probe__##_name(void *__data, _proto) \
632 struct lttng_event *__event = __data; \
633 struct lttng_channel *__chan = __event->chan; \
634 struct lib_ring_buffer_ctx __ctx; \
635 size_t __event_len, __event_align; \
636 size_t __dynamic_len_idx = 0; \
637 size_t __dynamic_len[ARRAY_SIZE(__event_fields___##_name)]; \
638 struct __event_typemap__##_name __typemap; \
642 (void) __dynamic_len_idx; /* don't warn if unused */ \
643 (void) __typemap; /* don't warn if unused */ \
645 if (!_TP_SESSION_CHECK(session, __chan->session)) \
647 if (unlikely(!ACCESS_ONCE(__chan->session->active))) \
649 if (unlikely(!ACCESS_ONCE(__chan->enabled))) \
651 if (unlikely(!ACCESS_ONCE(__event->enabled))) \
653 __event_len = __event_get_size__##_name(__dynamic_len, _args); \
654 __event_align = __event_get_align__##_name(_args); \
655 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
656 __event_align, -1); \
657 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
660 /* Control code (field ordering) */ \
662 __chan->ops->event_commit(&__ctx); \
664 /* Copy code, steered by control code */ \
668 #undef DECLARE_EVENT_CLASS_NOARGS
669 #define DECLARE_EVENT_CLASS_NOARGS(_name, _tstruct, _assign, _print) \
670 static void __event_probe__##_name(void *__data) \
672 struct lttng_event *__event = __data; \
673 struct lttng_channel *__chan = __event->chan; \
674 struct lib_ring_buffer_ctx __ctx; \
675 size_t __event_len, __event_align; \
678 if (!_TP_SESSION_CHECK(session, __chan->session)) \
680 if (unlikely(!ACCESS_ONCE(__chan->session->active))) \
682 if (unlikely(!ACCESS_ONCE(__chan->enabled))) \
684 if (unlikely(!ACCESS_ONCE(__event->enabled))) \
688 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
689 __event_align, -1); \
690 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
693 /* Control code (field ordering) */ \
695 __chan->ops->event_commit(&__ctx); \
697 /* Copy code, steered by control code */ \
701 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
704 * Stage 10 of the trace events.
706 * Register/unregister probes at module load/unload.
709 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
711 #define TP_ID1(_token, _system) _token##_system
712 #define TP_ID(_token, _system) TP_ID1(_token, _system)
713 #define module_init_eval1(_token, _system) module_init(_token##_system)
714 #define module_init_eval(_token, _system) module_init_eval1(_token, _system)
715 #define module_exit_eval1(_token, _system) module_exit(_token##_system)
716 #define module_exit_eval(_token, _system) module_exit_eval1(_token, _system)
718 #ifndef TP_MODULE_NOINIT
719 static int TP_ID(__lttng_events_init__
, TRACE_SYSTEM
)(void)
721 wrapper_vmalloc_sync_all();
722 return lttng_probe_register(&TP_ID(__probe_desc___
, TRACE_SYSTEM
));
725 static void TP_ID(__lttng_events_exit__
, TRACE_SYSTEM
)(void)
727 lttng_probe_unregister(&TP_ID(__probe_desc___
, TRACE_SYSTEM
));
730 #ifndef TP_MODULE_NOAUTOLOAD
731 module_init_eval(__lttng_events_init__
, TRACE_SYSTEM
);
732 module_exit_eval(__lttng_events_exit__
, TRACE_SYSTEM
);
737 #undef module_init_eval
738 #undef module_exit_eval
744 #undef TRACE_EVENT_FLAGS
This page took 0.049935 seconds and 6 git commands to generate.