Rename sys exit errno into ret
[lttng-modules.git] / probes / lttng-events.h
... / ...
CommitLineData
1/*
2 * lttng-events.h
3 *
4 * Copyright (C) 2009 Steven Rostedt <rostedt@goodmis.org>
5 * Copyright (C) 2010-2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 *
7 * Dual LGPL v2.1/GPL v2 license.
8 */
9
10#include <linux/debugfs.h>
11#include "lttng.h"
12#include "lttng-types.h"
13#include "../wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
14#include "../wrapper/ringbuffer/frontend_types.h"
15#include "../ltt-events.h"
16#include "../ltt-tracer-core.h"
17
18/*
19 * Macro declarations used for all stages.
20 */
21
22/*
23 * DECLARE_EVENT_CLASS can be used to add a generic function
24 * handlers for events. That is, if all events have the same
25 * parameters and just have distinct trace points.
26 * Each tracepoint can be defined with DEFINE_EVENT and that
27 * will map the DECLARE_EVENT_CLASS to the tracepoint.
28 *
29 * TRACE_EVENT is a one to one mapping between tracepoint and template.
30 */
31
32#undef TRACE_EVENT
33#define TRACE_EVENT(name, proto, args, tstruct, assign, print) \
34 DECLARE_EVENT_CLASS(name, \
35 PARAMS(proto), \
36 PARAMS(args), \
37 PARAMS(tstruct), \
38 PARAMS(assign), \
39 PARAMS(print)) \
40 DEFINE_EVENT(name, name, PARAMS(proto), PARAMS(args))
41
42#undef TRACE_EVENT_NOARGS
43#define TRACE_EVENT_NOARGS(name, tstruct, assign, print) \
44 DECLARE_EVENT_CLASS_NOARGS(name, \
45 PARAMS(tstruct), \
46 PARAMS(assign), \
47 PARAMS(print)) \
48 DEFINE_EVENT_NOARGS(name, name)
49
50
51#undef DEFINE_EVENT_PRINT
52#define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
53 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
54
55/* Callbacks are meaningless to LTTng. */
56#undef TRACE_EVENT_FN
57#define TRACE_EVENT_FN(name, proto, args, tstruct, \
58 assign, print, reg, unreg) \
59 TRACE_EVENT(name, PARAMS(proto), PARAMS(args), \
60 PARAMS(tstruct), PARAMS(assign), PARAMS(print)) \
61
62/*
63 * Stage 1 of the trace events.
64 *
65 * Create dummy trace calls for each events, verifying that the LTTng module
66 * TRACE_EVENT headers match the kernel arguments. Will be optimized out by the
67 * compiler.
68 */
69
70#include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
71
72#undef TP_PROTO
73#define TP_PROTO(args...) args
74
75#undef TP_ARGS
76#define TP_ARGS(args...) args
77
78#undef DEFINE_EVENT
79#define DEFINE_EVENT(_template, _name, _proto, _args) \
80void trace_##_name(_proto);
81
82#undef DEFINE_EVENT_NOARGS
83#define DEFINE_EVENT_NOARGS(_template, _name) \
84void trace_##_name(void *__data);
85
86#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
87
88/*
89 * Stage 2 of the trace events.
90 *
91 * Create event field type metadata section.
92 * Each event produce an array of fields.
93 */
94
95#include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
96
97/* Named field types must be defined in lttng-types.h */
98
99#undef __field_full
100#define __field_full(_type, _item, _order, _base) \
101 { \
102 .name = #_item, \
103 .type = __type_integer(_type, _order, _base, none), \
104 },
105
106#undef __field
107#define __field(_type, _item) \
108 __field_full(_type, _item, __BYTE_ORDER, 10)
109
110#undef __field_ext
111#define __field_ext(_type, _item, _filter_type) \
112 __field(_type, _item)
113
114#undef __field_network
115#define __field_network(_type, _item) \
116 __field_full(_type, _item, __BIG_ENDIAN, 10)
117
118#undef __field_network_hex
119#define __field_network_hex(_type, _item) \
120 __field_full(_type, _item, __BIG_ENDIAN, 16)
121
122#undef __array_enc_ext
123#define __array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
124 { \
125 .name = #_item, \
126 .type = \
127 { \
128 .atype = atype_array, \
129 .u.array = \
130 { \
131 .length = _length, \
132 .elem_type = __type_integer(_type, _order, _base, _encoding), \
133 }, \
134 }, \
135 },
136
137#undef __array
138#define __array(_type, _item, _length) \
139 __array_enc_ext(_type, _item, _length, __BYTE_ORDER, 10, none)
140
141#undef __array_text
142#define __array_text(_type, _item, _length) \
143 __array_enc_ext(_type, _item, _length, __BYTE_ORDER, 10, UTF8)
144
145#undef __array_hex
146#define __array_hex(_type, _item, _length) \
147 __array_enc_ext(_type, _item, _length, __BYTE_ORDER, 16, none)
148
149#undef __dynamic_array_enc_ext
150#define __dynamic_array_enc_ext(_type, _item, _length, _order, _base, _encoding) \
151 { \
152 .name = #_item, \
153 .type = \
154 { \
155 .atype = atype_sequence, \
156 .u.sequence = \
157 { \
158 .length_type = __type_integer(u32, __BYTE_ORDER, 10, none), \
159 .elem_type = __type_integer(_type, _order, _base, _encoding), \
160 }, \
161 }, \
162 },
163
164#undef __dynamic_array
165#define __dynamic_array(_type, _item, _length) \
166 __dynamic_array_enc_ext(_type, _item, _length, __BYTE_ORDER, 10, none)
167
168#undef __dynamic_array_text
169#define __dynamic_array_text(_type, _item, _length) \
170 __dynamic_array_enc_ext(_type, _item, _length, __BYTE_ORDER, 10, UTF8)
171
172#undef __dynamic_array_hex
173#define __dynamic_array_hex(_type, _item, _length) \
174 __dynamic_array_enc_ext(_type, _item, _length, __BYTE_ORDER, 16, none)
175
176#undef __string
177#define __string(_item, _src) \
178 { \
179 .name = #_item, \
180 .type = \
181 { \
182 .atype = atype_string, \
183 .u.basic.string.encoding = lttng_encode_UTF8, \
184 }, \
185 },
186
187#undef TP_STRUCT__entry
188#define TP_STRUCT__entry(args...) args /* Only one used in this phase */
189
190#undef DECLARE_EVENT_CLASS_NOARGS
191#define DECLARE_EVENT_CLASS_NOARGS(_name, _tstruct, _assign, _print) \
192 static const struct lttng_event_field __event_fields___##_name[] = { \
193 _tstruct \
194 };
195
196#undef DECLARE_EVENT_CLASS
197#define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
198 DECLARE_EVENT_CLASS_NOARGS(_name, PARAMS(_tstruct), PARAMS(_assign), \
199 PARAMS(_print))
200
201#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
202
203/*
204 * Stage 3 of the trace events.
205 *
206 * Create probe callback prototypes.
207 */
208
209#include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
210
211#undef TP_PROTO
212#define TP_PROTO(args...) args
213
214#undef DECLARE_EVENT_CLASS
215#define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
216static void __event_probe__##_name(void *__data, _proto);
217
218#undef DECLARE_EVENT_CLASS_NOARGS
219#define DECLARE_EVENT_CLASS_NOARGS(_name, _tstruct, _assign, _print) \
220static void __event_probe__##_name(void *__data);
221
222#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
223
224/*
225 * Stage 3.9 of the trace events.
226 *
227 * Create event descriptions.
228 */
229
230/* Named field types must be defined in lttng-types.h */
231
232#include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
233
234#ifndef TP_PROBE_CB
235#define TP_PROBE_CB(_template) &__event_probe__##_template
236#endif
237
238#undef DEFINE_EVENT_NOARGS
239#define DEFINE_EVENT_NOARGS(_template, _name) \
240static const struct lttng_event_desc __event_desc___##_name = { \
241 .fields = __event_fields___##_template, \
242 .name = #_name, \
243 .probe_callback = (void *) TP_PROBE_CB(_template), \
244 .nr_fields = ARRAY_SIZE(__event_fields___##_template), \
245 .owner = THIS_MODULE, \
246};
247
248#undef DEFINE_EVENT
249#define DEFINE_EVENT(_template, _name, _proto, _args) \
250 DEFINE_EVENT_NOARGS(_template, _name)
251
252#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
253
254
255/*
256 * Stage 4 of the trace events.
257 *
258 * Create an array of event description pointers.
259 */
260
261/* Named field types must be defined in lttng-types.h */
262
263#include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
264
265#undef DEFINE_EVENT_NOARGS
266#define DEFINE_EVENT_NOARGS(_template, _name) \
267 &__event_desc___##_name,
268
269#undef DEFINE_EVENT
270#define DEFINE_EVENT(_template, _name, _proto, _args) \
271 DEFINE_EVENT_NOARGS(_template, _name)
272
273#define TP_ID1(_token, _system) _token##_system
274#define TP_ID(_token, _system) TP_ID1(_token, _system)
275
276static const struct lttng_event_desc *TP_ID(__event_desc___, TRACE_SYSTEM)[] = {
277#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
278};
279
280#undef TP_ID1
281#undef TP_ID
282
283
284/*
285 * Stage 5 of the trace events.
286 *
287 * Create a toplevel descriptor for the whole probe.
288 */
289
290#define TP_ID1(_token, _system) _token##_system
291#define TP_ID(_token, _system) TP_ID1(_token, _system)
292
293/* non-const because list head will be modified when registered. */
294static __used struct lttng_probe_desc TP_ID(__probe_desc___, TRACE_SYSTEM) = {
295 .event_desc = TP_ID(__event_desc___, TRACE_SYSTEM),
296 .nr_events = ARRAY_SIZE(TP_ID(__event_desc___, TRACE_SYSTEM)),
297};
298
299#undef TP_ID1
300#undef TP_ID
301
302/*
303 * Stage 6 of the trace events.
304 *
305 * Create static inline function that calculates event size.
306 */
307
308#include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
309
310/* Named field types must be defined in lttng-types.h */
311
312#undef __field_full
313#define __field_full(_type, _item, _order, _base) \
314 __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(_type)); \
315 __event_len += sizeof(_type);
316
317#undef __array_enc_ext
318#define __array_enc_ext(_type, _item, _length, _order, _base, _encoding) \
319 __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(_type)); \
320 __event_len += sizeof(_type) * (_length);
321
322#undef __dynamic_array_enc_ext
323#define __dynamic_array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
324 __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(u32)); \
325 __event_len += sizeof(u32); \
326 __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(_type)); \
327 __dynamic_len[__dynamic_len_idx] = (_length); \
328 __event_len += sizeof(_type) * __dynamic_len[__dynamic_len_idx]; \
329 __dynamic_len_idx++;
330
331#undef __string
332#define __string(_item, _src) \
333 __event_len += __dynamic_len[__dynamic_len_idx++] = strlen(_src) + 1;
334
335#undef TP_PROTO
336#define TP_PROTO(args...) args
337
338#undef TP_STRUCT__entry
339#define TP_STRUCT__entry(args...) args
340
341#undef DECLARE_EVENT_CLASS
342#define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
343static inline size_t __event_get_size__##_name(size_t *__dynamic_len, _proto) \
344{ \
345 size_t __event_len = 0; \
346 unsigned int __dynamic_len_idx = 0; \
347 \
348 if (0) \
349 (void) __dynamic_len_idx; /* don't warn if unused */ \
350 _tstruct \
351 return __event_len; \
352}
353
354#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
355
356/*
357 * Stage 7 of the trace events.
358 *
359 * Create static inline function that calculates event payload alignment.
360 */
361
362#include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
363
364/* Named field types must be defined in lttng-types.h */
365
366#undef __field_full
367#define __field_full(_type, _item, _order, _base) \
368 __event_align = max_t(size_t, __event_align, ltt_alignof(_type));
369
370#undef __array_enc_ext
371#define __array_enc_ext(_type, _item, _length, _order, _base, _encoding) \
372 __event_align = max_t(size_t, __event_align, ltt_alignof(_type));
373
374#undef __dynamic_array_enc_ext
375#define __dynamic_array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
376 __event_align = max_t(size_t, __event_align, ltt_alignof(u32)); \
377 __event_align = max_t(size_t, __event_align, ltt_alignof(_type));
378
379#undef __string
380#define __string(_item, _src)
381
382#undef TP_PROTO
383#define TP_PROTO(args...) args
384
385#undef TP_STRUCT__entry
386#define TP_STRUCT__entry(args...) args
387
388#undef DECLARE_EVENT_CLASS
389#define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
390static inline size_t __event_get_align__##_name(_proto) \
391{ \
392 size_t __event_align = 1; \
393 _tstruct \
394 return __event_align; \
395}
396
397#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
398
399
400/*
401 * Stage 8 of the trace events.
402 *
403 * Create structure declaration that allows the "assign" macros to access the
404 * field types.
405 */
406
407#include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
408
409/* Named field types must be defined in lttng-types.h */
410
411#undef __field_full
412#define __field_full(_type, _item, _order, _base) _type _item;
413
414#undef __array_enc_ext
415#define __array_enc_ext(_type, _item, _length, _order, _base, _encoding) \
416 _type _item;
417
418#undef __dynamic_array_enc_ext
419#define __dynamic_array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
420 _type _item;
421
422#undef __string
423#define __string(_item, _src) char _item;
424
425#undef TP_STRUCT__entry
426#define TP_STRUCT__entry(args...) args
427
428#undef DECLARE_EVENT_CLASS
429#define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
430struct __event_typemap__##_name { \
431 _tstruct \
432};
433
434#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
435
436
437/*
438 * Stage 9 of the trace events.
439 *
440 * Create the probe function : call even size calculation and write event data
441 * into the buffer.
442 *
443 * We use both the field and assignment macros to write the fields in the order
444 * defined in the field declaration. The field declarations control the
445 * execution order, jumping to the appropriate assignment block.
446 */
447
448#include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
449
450#undef __field_full
451#define __field_full(_type, _item, _order, _base) \
452 goto __assign_##_item; \
453__end_field_##_item:
454
455#undef __array_enc_ext
456#define __array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
457 goto __assign_##_item; \
458__end_field_##_item:
459
460#undef __dynamic_array_enc_ext
461#define __dynamic_array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
462 goto __assign_##_item##_1; \
463__end_field_##_item##_1: \
464 goto __assign_##_item##_2; \
465__end_field_##_item##_2:
466
467#undef __string
468#define __string(_item, _src) \
469 goto __assign_##_item; \
470__end_field_##_item:
471
472/*
473 * Macros mapping tp_assign() to "=", tp_memcpy() to memcpy() and tp_strcpy() to
474 * strcpy().
475 */
476#undef tp_assign
477#define tp_assign(dest, src) \
478__assign_##dest: \
479 { \
480 __typeof__(__typemap.dest) __tmp = (src); \
481 lib_ring_buffer_align_ctx(&__ctx, ltt_alignof(__tmp)); \
482 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
483 } \
484 goto __end_field_##dest;
485
486#undef tp_memcpy
487#define tp_memcpy(dest, src, len) \
488__assign_##dest: \
489 if (0) \
490 (void) __typemap.dest; \
491 lib_ring_buffer_align_ctx(&__ctx, ltt_alignof(__typemap.dest)); \
492 __chan->ops->event_write(&__ctx, src, len); \
493 goto __end_field_##dest;
494
495#undef tp_memcpy_dyn
496#define tp_memcpy_dyn(dest, src) \
497__assign_##dest##_1: \
498 { \
499 u32 __tmpl = __dynamic_len[__dynamic_len_idx]; \
500 lib_ring_buffer_align_ctx(&__ctx, ltt_alignof(u32)); \
501 __chan->ops->event_write(&__ctx, &__tmpl, sizeof(u32)); \
502 } \
503 goto __end_field_##dest##_1; \
504__assign_##dest##_2: \
505 lib_ring_buffer_align_ctx(&__ctx, ltt_alignof(__typemap.dest)); \
506 __chan->ops->event_write(&__ctx, src, \
507 sizeof(__typemap.dest) * __get_dynamic_array_len(dest));\
508 goto __end_field_##dest##_2;
509
510#undef tp_strcpy
511#define tp_strcpy(dest, src) \
512 tp_memcpy(dest, src, __get_dynamic_array_len(dest))
513
514/* Named field types must be defined in lttng-types.h */
515
516#undef __get_str
517#define __get_str(field) field
518
519#undef __get_dynamic_array
520#define __get_dynamic_array(field) field
521
522/* Beware: this get len actually consumes the len value */
523#undef __get_dynamic_array_len
524#define __get_dynamic_array_len(field) __dynamic_len[__dynamic_len_idx++]
525
526#undef TP_PROTO
527#define TP_PROTO(args...) args
528
529#undef TP_ARGS
530#define TP_ARGS(args...) args
531
532#undef TP_STRUCT__entry
533#define TP_STRUCT__entry(args...) args
534
535#undef TP_fast_assign
536#define TP_fast_assign(args...) args
537
538#undef DECLARE_EVENT_CLASS
539#define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
540static void __event_probe__##_name(void *__data, _proto) \
541{ \
542 struct ltt_event *__event = __data; \
543 struct ltt_channel *__chan = __event->chan; \
544 struct lib_ring_buffer_ctx __ctx; \
545 size_t __event_len, __event_align; \
546 size_t __dynamic_len_idx = 0; \
547 size_t __dynamic_len[ARRAY_SIZE(__event_fields___##_name)]; \
548 struct __event_typemap__##_name __typemap; \
549 int __ret; \
550 \
551 if (0) \
552 (void) __dynamic_len_idx; /* don't warn if unused */ \
553 if (unlikely(!ACCESS_ONCE(__chan->session->active))) \
554 return; \
555 if (unlikely(!ACCESS_ONCE(__chan->enabled))) \
556 return; \
557 if (unlikely(!ACCESS_ONCE(__event->enabled))) \
558 return; \
559 __event_len = __event_get_size__##_name(__dynamic_len, _args); \
560 __event_align = __event_get_align__##_name(_args); \
561 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
562 __event_align, -1); \
563 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
564 if (__ret < 0) \
565 return; \
566 /* Control code (field ordering) */ \
567 _tstruct \
568 __chan->ops->event_commit(&__ctx); \
569 return; \
570 /* Copy code, steered by control code */ \
571 _assign \
572}
573
574#undef DECLARE_EVENT_CLASS_NOARGS
575#define DECLARE_EVENT_CLASS_NOARGS(_name, _tstruct, _assign, _print) \
576static void __event_probe__##_name(void *__data) \
577{ \
578 struct ltt_event *__event = __data; \
579 struct ltt_channel *__chan = __event->chan; \
580 struct lib_ring_buffer_ctx __ctx; \
581 size_t __event_len, __event_align; \
582 int __ret; \
583 \
584 if (unlikely(!ACCESS_ONCE(__chan->session->active))) \
585 return; \
586 if (unlikely(!ACCESS_ONCE(__chan->enabled))) \
587 return; \
588 if (unlikely(!ACCESS_ONCE(__event->enabled))) \
589 return; \
590 __event_len = 0; \
591 __event_align = 1; \
592 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
593 __event_align, -1); \
594 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
595 if (__ret < 0) \
596 return; \
597 /* Control code (field ordering) */ \
598 _tstruct \
599 __chan->ops->event_commit(&__ctx); \
600 return; \
601 /* Copy code, steered by control code */ \
602 _assign \
603}
604
605#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
606
607/*
608 * Stage 10 of the trace events.
609 *
610 * Register/unregister probes at module load/unload.
611 */
612
613#include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
614
615#define TP_ID1(_token, _system) _token##_system
616#define TP_ID(_token, _system) TP_ID1(_token, _system)
617#define module_init_eval1(_token, _system) module_init(_token##_system)
618#define module_init_eval(_token, _system) module_init_eval1(_token, _system)
619#define module_exit_eval1(_token, _system) module_exit(_token##_system)
620#define module_exit_eval(_token, _system) module_exit_eval1(_token, _system)
621
622#ifndef TP_MODULE_OVERRIDE
623static int TP_ID(__lttng_events_init__, TRACE_SYSTEM)(void)
624{
625 wrapper_vmalloc_sync_all();
626 return ltt_probe_register(&TP_ID(__probe_desc___, TRACE_SYSTEM));
627}
628
629module_init_eval(__lttng_events_init__, TRACE_SYSTEM);
630
631static void TP_ID(__lttng_events_exit__, TRACE_SYSTEM)(void)
632{
633 ltt_probe_unregister(&TP_ID(__probe_desc___, TRACE_SYSTEM));
634}
635
636module_exit_eval(__lttng_events_exit__, TRACE_SYSTEM);
637#endif
638
639#undef module_init_eval
640#undef module_exit_eval
641#undef TP_ID1
642#undef TP_ID
643
644#undef TP_PROTO
645#undef TP_ARGS
646#undef TRACE_EVENT_FLAGS
This page took 0.024366 seconds and 4 git commands to generate.