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