Support generation of syscall probes for those with pointers
[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#undef __type_integer
204
205/*
206 * Stage 3 of the trace events.
207 *
208 * Create probe callback prototypes.
209 */
210
211#include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
212
213#undef TP_PROTO
214#define TP_PROTO(args...) args
215
216#undef DECLARE_EVENT_CLASS
217#define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
218static void __event_probe__##_name(void *__data, _proto);
219
220#undef DECLARE_EVENT_CLASS_NOARGS
221#define DECLARE_EVENT_CLASS_NOARGS(_name, _tstruct, _assign, _print) \
222static void __event_probe__##_name(void *__data);
223
224#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
225
226/*
227 * Stage 3.9 of the trace events.
228 *
229 * Create event descriptions.
230 */
231
232/* Named field types must be defined in lttng-types.h */
233
234#include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
235
236#ifndef TP_PROBE_CB
237#define TP_PROBE_CB(_template) &__event_probe__##_template
238#endif
239
240#undef DEFINE_EVENT_NOARGS
241#define DEFINE_EVENT_NOARGS(_template, _name) \
242static const struct lttng_event_desc __event_desc___##_name = { \
243 .fields = __event_fields___##_template, \
244 .name = #_name, \
245 .probe_callback = (void *) TP_PROBE_CB(_template), \
246 .nr_fields = ARRAY_SIZE(__event_fields___##_template), \
247 .owner = THIS_MODULE, \
248};
249
250#undef DEFINE_EVENT
251#define DEFINE_EVENT(_template, _name, _proto, _args) \
252 DEFINE_EVENT_NOARGS(_template, _name)
253
254#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
255
256
257/*
258 * Stage 4 of the trace events.
259 *
260 * Create an array of event description pointers.
261 */
262
263/* Named field types must be defined in lttng-types.h */
264
265#include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
266
267#undef DEFINE_EVENT_NOARGS
268#define DEFINE_EVENT_NOARGS(_template, _name) \
269 &__event_desc___##_name,
270
271#undef DEFINE_EVENT
272#define DEFINE_EVENT(_template, _name, _proto, _args) \
273 DEFINE_EVENT_NOARGS(_template, _name)
274
275#define TP_ID1(_token, _system) _token##_system
276#define TP_ID(_token, _system) TP_ID1(_token, _system)
277
278static const struct lttng_event_desc *TP_ID(__event_desc___, TRACE_SYSTEM)[] = {
279#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
280};
281
282#undef TP_ID1
283#undef TP_ID
284
285
286/*
287 * Stage 5 of the trace events.
288 *
289 * Create a toplevel descriptor for the whole probe.
290 */
291
292#define TP_ID1(_token, _system) _token##_system
293#define TP_ID(_token, _system) TP_ID1(_token, _system)
294
295/* non-const because list head will be modified when registered. */
296static __used struct lttng_probe_desc TP_ID(__probe_desc___, TRACE_SYSTEM) = {
297 .event_desc = TP_ID(__event_desc___, TRACE_SYSTEM),
298 .nr_events = ARRAY_SIZE(TP_ID(__event_desc___, TRACE_SYSTEM)),
299};
300
301#undef TP_ID1
302#undef TP_ID
303
304/*
305 * Stage 6 of the trace events.
306 *
307 * Create static inline function that calculates event size.
308 */
309
310#include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
311
312/* Named field types must be defined in lttng-types.h */
313
314#undef __field_full
315#define __field_full(_type, _item, _order, _base) \
316 __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(_type)); \
317 __event_len += sizeof(_type);
318
319#undef __array_enc_ext
320#define __array_enc_ext(_type, _item, _length, _order, _base, _encoding) \
321 __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(_type)); \
322 __event_len += sizeof(_type) * (_length);
323
324#undef __dynamic_array_enc_ext
325#define __dynamic_array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
326 __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(u32)); \
327 __event_len += sizeof(u32); \
328 __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(_type)); \
329 __dynamic_len[__dynamic_len_idx] = (_length); \
330 __event_len += sizeof(_type) * __dynamic_len[__dynamic_len_idx]; \
331 __dynamic_len_idx++;
332
333#undef __string
334#define __string(_item, _src) \
335 __event_len += __dynamic_len[__dynamic_len_idx++] = strlen(_src) + 1;
336
337#undef TP_PROTO
338#define TP_PROTO(args...) args
339
340#undef TP_STRUCT__entry
341#define TP_STRUCT__entry(args...) args
342
343#undef DECLARE_EVENT_CLASS
344#define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
345static inline size_t __event_get_size__##_name(size_t *__dynamic_len, _proto) \
346{ \
347 size_t __event_len = 0; \
348 unsigned int __dynamic_len_idx = 0; \
349 \
350 if (0) \
351 (void) __dynamic_len_idx; /* don't warn if unused */ \
352 _tstruct \
353 return __event_len; \
354}
355
356#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
357
358/*
359 * Stage 7 of the trace events.
360 *
361 * Create static inline function that calculates event payload alignment.
362 */
363
364#include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
365
366/* Named field types must be defined in lttng-types.h */
367
368#undef __field_full
369#define __field_full(_type, _item, _order, _base) \
370 __event_align = max_t(size_t, __event_align, ltt_alignof(_type));
371
372#undef __array_enc_ext
373#define __array_enc_ext(_type, _item, _length, _order, _base, _encoding) \
374 __event_align = max_t(size_t, __event_align, ltt_alignof(_type));
375
376#undef __dynamic_array_enc_ext
377#define __dynamic_array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
378 __event_align = max_t(size_t, __event_align, ltt_alignof(u32)); \
379 __event_align = max_t(size_t, __event_align, ltt_alignof(_type));
380
381#undef __string
382#define __string(_item, _src)
383
384#undef TP_PROTO
385#define TP_PROTO(args...) args
386
387#undef TP_STRUCT__entry
388#define TP_STRUCT__entry(args...) args
389
390#undef DECLARE_EVENT_CLASS
391#define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
392static inline size_t __event_get_align__##_name(_proto) \
393{ \
394 size_t __event_align = 1; \
395 _tstruct \
396 return __event_align; \
397}
398
399#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
400
401
402/*
403 * Stage 8 of the trace events.
404 *
405 * Create structure declaration that allows the "assign" macros to access the
406 * field types.
407 */
408
409#include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
410
411/* Named field types must be defined in lttng-types.h */
412
413#undef __field_full
414#define __field_full(_type, _item, _order, _base) _type _item;
415
416#undef __array_enc_ext
417#define __array_enc_ext(_type, _item, _length, _order, _base, _encoding) \
418 _type _item;
419
420#undef __dynamic_array_enc_ext
421#define __dynamic_array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
422 _type _item;
423
424#undef __string
425#define __string(_item, _src) char _item;
426
427#undef TP_STRUCT__entry
428#define TP_STRUCT__entry(args...) args
429
430#undef DECLARE_EVENT_CLASS
431#define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
432struct __event_typemap__##_name { \
433 _tstruct \
434};
435
436#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
437
438
439/*
440 * Stage 9 of the trace events.
441 *
442 * Create the probe function : call even size calculation and write event data
443 * into the buffer.
444 *
445 * We use both the field and assignment macros to write the fields in the order
446 * defined in the field declaration. The field declarations control the
447 * execution order, jumping to the appropriate assignment block.
448 */
449
450#include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
451
452#undef __field_full
453#define __field_full(_type, _item, _order, _base) \
454 goto __assign_##_item; \
455__end_field_##_item:
456
457#undef __array_enc_ext
458#define __array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
459 goto __assign_##_item; \
460__end_field_##_item:
461
462#undef __dynamic_array_enc_ext
463#define __dynamic_array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
464 goto __assign_##_item##_1; \
465__end_field_##_item##_1: \
466 goto __assign_##_item##_2; \
467__end_field_##_item##_2:
468
469#undef __string
470#define __string(_item, _src) \
471 goto __assign_##_item; \
472__end_field_##_item:
473
474/*
475 * Macros mapping tp_assign() to "=", tp_memcpy() to memcpy() and tp_strcpy() to
476 * strcpy().
477 */
478#undef tp_assign
479#define tp_assign(dest, src) \
480__assign_##dest: \
481 { \
482 __typeof__(__typemap.dest) __tmp = (src); \
483 lib_ring_buffer_align_ctx(&__ctx, ltt_alignof(__tmp)); \
484 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
485 } \
486 goto __end_field_##dest;
487
488#undef tp_memcpy
489#define tp_memcpy(dest, src, len) \
490__assign_##dest: \
491 if (0) \
492 (void) __typemap.dest; \
493 lib_ring_buffer_align_ctx(&__ctx, ltt_alignof(__typemap.dest)); \
494 __chan->ops->event_write(&__ctx, src, len); \
495 goto __end_field_##dest;
496
497#undef tp_memcpy_dyn
498#define tp_memcpy_dyn(dest, src) \
499__assign_##dest##_1: \
500 { \
501 u32 __tmpl = __dynamic_len[__dynamic_len_idx]; \
502 lib_ring_buffer_align_ctx(&__ctx, ltt_alignof(u32)); \
503 __chan->ops->event_write(&__ctx, &__tmpl, sizeof(u32)); \
504 } \
505 goto __end_field_##dest##_1; \
506__assign_##dest##_2: \
507 lib_ring_buffer_align_ctx(&__ctx, ltt_alignof(__typemap.dest)); \
508 __chan->ops->event_write(&__ctx, src, \
509 sizeof(__typemap.dest) * __get_dynamic_array_len(dest));\
510 goto __end_field_##dest##_2;
511
512#undef tp_strcpy
513#define tp_strcpy(dest, src) \
514 tp_memcpy(dest, src, __get_dynamic_array_len(dest))
515
516/* Named field types must be defined in lttng-types.h */
517
518#undef __get_str
519#define __get_str(field) field
520
521#undef __get_dynamic_array
522#define __get_dynamic_array(field) field
523
524/* Beware: this get len actually consumes the len value */
525#undef __get_dynamic_array_len
526#define __get_dynamic_array_len(field) __dynamic_len[__dynamic_len_idx++]
527
528#undef TP_PROTO
529#define TP_PROTO(args...) args
530
531#undef TP_ARGS
532#define TP_ARGS(args...) args
533
534#undef TP_STRUCT__entry
535#define TP_STRUCT__entry(args...) args
536
537#undef TP_fast_assign
538#define TP_fast_assign(args...) args
539
540#undef DECLARE_EVENT_CLASS
541#define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
542static void __event_probe__##_name(void *__data, _proto) \
543{ \
544 struct ltt_event *__event = __data; \
545 struct ltt_channel *__chan = __event->chan; \
546 struct lib_ring_buffer_ctx __ctx; \
547 size_t __event_len, __event_align; \
548 size_t __dynamic_len_idx = 0; \
549 size_t __dynamic_len[ARRAY_SIZE(__event_fields___##_name)]; \
550 struct __event_typemap__##_name __typemap; \
551 int __ret; \
552 \
553 if (0) \
554 (void) __dynamic_len_idx; /* don't warn if unused */ \
555 if (unlikely(!ACCESS_ONCE(__chan->session->active))) \
556 return; \
557 if (unlikely(!ACCESS_ONCE(__chan->enabled))) \
558 return; \
559 if (unlikely(!ACCESS_ONCE(__event->enabled))) \
560 return; \
561 __event_len = __event_get_size__##_name(__dynamic_len, _args); \
562 __event_align = __event_get_align__##_name(_args); \
563 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
564 __event_align, -1); \
565 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
566 if (__ret < 0) \
567 return; \
568 /* Control code (field ordering) */ \
569 _tstruct \
570 __chan->ops->event_commit(&__ctx); \
571 return; \
572 /* Copy code, steered by control code */ \
573 _assign \
574}
575
576#undef DECLARE_EVENT_CLASS_NOARGS
577#define DECLARE_EVENT_CLASS_NOARGS(_name, _tstruct, _assign, _print) \
578static void __event_probe__##_name(void *__data) \
579{ \
580 struct ltt_event *__event = __data; \
581 struct ltt_channel *__chan = __event->chan; \
582 struct lib_ring_buffer_ctx __ctx; \
583 size_t __event_len, __event_align; \
584 int __ret; \
585 \
586 if (unlikely(!ACCESS_ONCE(__chan->session->active))) \
587 return; \
588 if (unlikely(!ACCESS_ONCE(__chan->enabled))) \
589 return; \
590 if (unlikely(!ACCESS_ONCE(__event->enabled))) \
591 return; \
592 __event_len = 0; \
593 __event_align = 1; \
594 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
595 __event_align, -1); \
596 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
597 if (__ret < 0) \
598 return; \
599 /* Control code (field ordering) */ \
600 _tstruct \
601 __chan->ops->event_commit(&__ctx); \
602 return; \
603 /* Copy code, steered by control code */ \
604 _assign \
605}
606
607#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
608
609/*
610 * Stage 10 of the trace events.
611 *
612 * Register/unregister probes at module load/unload.
613 */
614
615#include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
616
617#define TP_ID1(_token, _system) _token##_system
618#define TP_ID(_token, _system) TP_ID1(_token, _system)
619#define module_init_eval1(_token, _system) module_init(_token##_system)
620#define module_init_eval(_token, _system) module_init_eval1(_token, _system)
621#define module_exit_eval1(_token, _system) module_exit(_token##_system)
622#define module_exit_eval(_token, _system) module_exit_eval1(_token, _system)
623
624#ifndef TP_MODULE_OVERRIDE
625static int TP_ID(__lttng_events_init__, TRACE_SYSTEM)(void)
626{
627 wrapper_vmalloc_sync_all();
628 return ltt_probe_register(&TP_ID(__probe_desc___, TRACE_SYSTEM));
629}
630
631module_init_eval(__lttng_events_init__, TRACE_SYSTEM);
632
633static void TP_ID(__lttng_events_exit__, TRACE_SYSTEM)(void)
634{
635 ltt_probe_unregister(&TP_ID(__probe_desc___, TRACE_SYSTEM));
636}
637
638module_exit_eval(__lttng_events_exit__, TRACE_SYSTEM);
639#endif
640
641#undef module_init_eval
642#undef module_exit_eval
643#undef TP_ID1
644#undef TP_ID
This page took 0.024554 seconds and 4 git commands to generate.