Tracepoints: add provider mismatch check
[lttng-ust.git] / include / lttng / ust-tracepoint-event.h
1 /*
2 * Copyright (C) 2009 Steven Rostedt <srostedt@redhat.com>
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation;
8 * version 2.1 of the License.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include <stdio.h>
21 #include <urcu/compiler.h>
22 #include <lttng/ust-events.h>
23 #include <lttng/usterr-signal-safe.h>
24 #include <lttng/ringbuffer-config.h>
25
26 /*
27 * Macro declarations used for all stages.
28 */
29
30 #undef ctf_integer
31 #define ctf_integer(_type, _item, _src) \
32 ctf_integer_ext(_type, _item, _src, BYTE_ORDER, 10)
33
34 #undef ctf_integer_hex
35 #define ctf_integer_hex(_type, _item, _src) \
36 ctf_integer_ext(_type, _item, _src, BYTE_ORDER, 16)
37
38 #undef ctf_integer_network
39 #define ctf_integer_network(_type, _item, _src) \
40 ctf_integer_ext(_type, _item, _src, BIG_ENDIAN, 10)
41
42 #undef ctf_integer_network_hex
43 #define ctf_integer_network_hex(_type, _item, _src) \
44 ctf_integer_ext(_type, _item, _src, BIG_ENDIAN, 16)
45
46 /* ctf_float is redefined at each step */
47
48 #undef ctf_array
49 #define ctf_array(_type, _item, _src, _length) \
50 ctf_array_encoded(_type, _item, _src, _length, none)
51
52 #undef ctf_array_text
53 #define ctf_array_text(_type, _item, _src, _length) \
54 ctf_array_encoded(_type, _item, _src, _length, UTF8)
55
56 #undef ctf_sequence
57 #define ctf_sequence(_type, _item, _src, _length_type, _src_length) \
58 ctf_sequence_encoded(_type, _item, _src, \
59 _length_type, _src_length, none)
60
61 #undef ctf_sequence_text
62 #define ctf_sequence_text(_type, _item, _src, _length_type, _src_length) \
63 ctf_sequence_encoded(_type, _item, _src, \
64 _length_type, _src_length, UTF8)
65
66 /* ctf_string is redefined at each step */
67
68 /*
69 * TRACEPOINT_EVENT_CLASS can be used to add a generic function handlers
70 * for events. That is, if all events have the same parameters and just
71 * have distinct trace points. Each tracepoint can be defined with
72 * TRACEPOINT_EVENT_INSTANCE and that will map the
73 * TRACEPOINT_EVENT_CLASS to the tracepoint.
74 *
75 * TRACEPOINT_EVENT is a one to one mapping between tracepoint and
76 * template.
77 */
78
79 #undef TRACEPOINT_EVENT
80 #define TRACEPOINT_EVENT(provider, name, proto, args, fields) \
81 TRACEPOINT_EVENT_CLASS(provider, name, \
82 TP_PARAMS(proto), \
83 TP_PARAMS(args), \
84 TP_PARAMS(fields)) \
85 TRACEPOINT_EVENT_INSTANCE(provider, name, name, TP_PARAMS(proto), TP_PARAMS(args))
86
87 #undef TRACEPOINT_EVENT_NOARGS
88 #define TRACEPOINT_EVENT_NOARGS(provider, name, fields) \
89 TRACEPOINT_EVENT_CLASS_NOARGS(provider, name, \
90 TP_PARAMS(fields)) \
91 TRACEPOINT_EVENT_INSTANCE_NOARGS(provider, name, name)
92
93 /* Helpers */
94 #define _TP_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
95
96 #define _tp_max_t(type, x, y) \
97 ({ \
98 type __max1 = (x); \
99 type __max2 = (y); \
100 __max1 > __max2 ? __max1: __max2; \
101 })
102
103 /*
104 * Stage 0 of the trace events.
105 *
106 * Check that each TRACEPOINT_EVENT provider argument match the
107 * TRACEPOINT_PROVIDER by creating dummy callbacks.
108 */
109
110 /* Reset all macros within TRACEPOINT_EVENT */
111 #include <lttng/ust-tracepoint-event-reset.h>
112
113 #define TP_ID1(_token, _provider) _token##_provider
114 #define TP_ID(_token, _provider) TP_ID1(_token, _provider)
115
116 static inline
117 void TP_ID(__tracepoint_provider_mismatch_, TRACEPOINT_PROVIDER)(void)
118 {
119 }
120
121 #undef TRACEPOINT_EVENT_CLASS
122 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _proto, _args, _fields) \
123 __tracepoint_provider_mismatch_##_provider();
124
125 #undef TRACEPOINT_EVENT_CLASS_NOARGS
126 #define TRACEPOINT_EVENT_CLASS_NOARGS(_provider, _name, _fields) \
127 __tracepoint_provider_mismatch_##_provider();
128
129 #undef TRACEPOINT_EVENT_INSTANCE
130 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _proto, _args) \
131 __tracepoint_provider_mismatch_##_provider();
132
133 #undef TRACEPOINT_EVENT_INSTANCE_NOARGS
134 #define TRACEPOINT_EVENT_INSTANCE_NOARGS(_provider, _template, _name) \
135 __tracepoint_provider_mismatch_##_provider();
136
137 static __attribute__((unused))
138 void TP_ID(__tracepoint_provider_check_, TRACEPOINT_PROVIDER)(void)
139 {
140 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
141 }
142
143 #undef TP_ID1
144 #undef TP_ID
145
146 /*
147 * Stage 1 of the trace events.
148 *
149 * Create event field type metadata section.
150 * Each event produce an array of fields.
151 */
152
153 /* Reset all macros within TRACEPOINT_EVENT */
154 #include <lttng/ust-tracepoint-event-reset.h>
155
156 #undef ctf_integer_ext
157 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
158 { \
159 .name = #_item, \
160 .type = __type_integer(_type, _byte_order, _base, none),\
161 },
162
163 #undef ctf_float
164 #define ctf_float(_type, _item, _src) \
165 { \
166 .name = #_item, \
167 .type = __type_float(_type), \
168 },
169
170 #undef ctf_array_encoded
171 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
172 { \
173 .name = #_item, \
174 .type = \
175 { \
176 .atype = atype_array, \
177 .u.array = \
178 { \
179 .length = _length, \
180 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
181 }, \
182 }, \
183 },
184
185 #undef ctf_sequence_encoded
186 #define ctf_sequence_encoded(_type, _item, _src, \
187 _length_type, _src_length, _encoding) \
188 { \
189 .name = #_item, \
190 .type = \
191 { \
192 .atype = atype_sequence, \
193 .u.sequence = \
194 { \
195 .length_type = __type_integer(_length_type, BYTE_ORDER, 10, none), \
196 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
197 }, \
198 }, \
199 },
200
201 #undef ctf_string
202 #define ctf_string(_item, _src) \
203 { \
204 .name = #_item, \
205 .type = \
206 { \
207 .atype = atype_string, \
208 .u.basic.string.encoding = lttng_encode_UTF8, \
209 }, \
210 },
211
212 #undef TP_FIELDS
213 #define TP_FIELDS(args...) args /* Only one used in this phase */
214
215 #undef TRACEPOINT_EVENT_CLASS_NOARGS
216 #define TRACEPOINT_EVENT_CLASS_NOARGS(_provider, _name, _fields) \
217 static const struct lttng_event_field __event_fields___##_provider##___##_name[] = { \
218 _fields \
219 };
220
221 #undef TRACEPOINT_EVENT_CLASS
222 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _proto, _args, _fields) \
223 TRACEPOINT_EVENT_CLASS_NOARGS(_provider, _name, TP_PARAMS(_fields))
224
225 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
226
227 /*
228 * Stage 2 of the trace events.
229 *
230 * Create probe callback prototypes.
231 */
232
233 /* Reset all macros within TRACEPOINT_EVENT */
234 #include <lttng/ust-tracepoint-event-reset.h>
235
236 #undef TP_PROTO
237 #define TP_PROTO(args...) args
238
239 #undef TRACEPOINT_EVENT_CLASS
240 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _proto, _args, _fields) \
241 static void __event_probe__##_provider##___##_name(void *__data, _proto);
242
243 #undef TRACEPOINT_EVENT_CLASS_NOARGS
244 #define TRACEPOINT_EVENT_CLASS_NOARGS(_provider, _name, _fields) \
245 static void __event_probe__##_provider##___##_name(void *__data);
246
247 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
248
249 /*
250 * Stage 3 of the trace events.
251 *
252 * Create an array of events.
253 */
254
255 /* Reset all macros within TRACEPOINT_EVENT */
256 #include <lttng/ust-tracepoint-event-reset.h>
257
258 #undef TRACEPOINT_EVENT_INSTANCE_NOARGS
259 #define TRACEPOINT_EVENT_INSTANCE_NOARGS(_provider, _template, _name) \
260 { \
261 .fields = __event_fields___##_provider##___##_template,\
262 .name = #_provider ":" #_name, \
263 .probe_callback = (void *) &__event_probe__##_provider##___##_template,\
264 .nr_fields = _TP_ARRAY_SIZE(__event_fields___##_provider##___##_template), \
265 },
266
267 #undef TRACEPOINT_EVENT_INSTANCE
268 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _proto, _args) \
269 TRACEPOINT_EVENT_INSTANCE_NOARGS(_provider, _template, _name)
270
271 #define TP_ID1(_token, _provider) _token##_provider
272 #define TP_ID(_token, _provider) TP_ID1(_token, _provider)
273
274 static const struct lttng_event_desc TP_ID(__event_desc___, TRACEPOINT_PROVIDER)[] = {
275 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
276 };
277
278 #undef TP_ID1
279 #undef TP_ID
280
281
282 /*
283 * Stage 4 of the trace events.
284 *
285 * Create a toplevel descriptor for the whole probe.
286 */
287
288 #define TP_ID1(_token, _provider) _token##_provider
289 #define TP_ID(_token, _provider) TP_ID1(_token, _provider)
290
291 /* non-const because list head will be modified when registered. */
292 static struct lttng_probe_desc TP_ID(__probe_desc___, TRACEPOINT_PROVIDER) = {
293 .event_desc = TP_ID(__event_desc___, TRACEPOINT_PROVIDER),
294 .nr_events = _TP_ARRAY_SIZE(TP_ID(__event_desc___, TRACEPOINT_PROVIDER)),
295 };
296
297 #undef TP_ID1
298 #undef TP_ID
299
300 /*
301 * Stage 5 of the trace events.
302 *
303 * Create static inline function that calculates event size.
304 */
305
306 /* Reset all macros within TRACEPOINT_EVENT */
307 #include <lttng/ust-tracepoint-event-reset.h>
308
309 #undef ctf_integer_ext
310 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
311 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
312 __event_len += sizeof(_type);
313
314 #undef ctf_float
315 #define ctf_float(_type, _item, _src) \
316 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
317 __event_len += sizeof(_type);
318
319 #undef ctf_array_encoded
320 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
321 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
322 __event_len += sizeof(_type) * (_length);
323
324 #undef ctf_sequence_encoded
325 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
326 _src_length, _encoding) \
327 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_length_type)); \
328 __event_len += sizeof(_length_type); \
329 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
330 __dynamic_len[__dynamic_len_idx] = (_src_length); \
331 __event_len += sizeof(_type) * __dynamic_len[__dynamic_len_idx]; \
332 __dynamic_len_idx++;
333
334 #undef ctf_string
335 #define ctf_string(_item, _src) \
336 __event_len += __dynamic_len[__dynamic_len_idx++] = strlen(_src) + 1;
337
338 #undef TP_PROTO
339 #define TP_PROTO(args...) args
340
341 #undef TP_FIELDS
342 #define TP_FIELDS(args...) args
343
344 #undef TRACEPOINT_EVENT_CLASS
345 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _proto, _args, _fields) \
346 static inline size_t __event_get_size__##_provider##___##_name(size_t *__dynamic_len, _proto) \
347 { \
348 size_t __event_len = 0; \
349 unsigned int __dynamic_len_idx = 0; \
350 \
351 if (0) \
352 (void) __dynamic_len_idx; /* don't warn if unused */ \
353 _fields \
354 return __event_len; \
355 }
356
357 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
358
359 /*
360 * Stage 6 of the trace events.
361 *
362 * Create static inline function that calculates event payload alignment.
363 */
364
365 /* Reset all macros within TRACEPOINT_EVENT */
366 #include <lttng/ust-tracepoint-event-reset.h>
367
368 #undef ctf_integer_ext
369 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
370 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
371
372 #undef ctf_float
373 #define ctf_float(_type, _item, _src) \
374 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
375
376 #undef ctf_array_encoded
377 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
378 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
379
380 #undef ctf_sequence_encoded
381 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
382 _src_length, _encoding) \
383 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_length_type)); \
384 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
385
386 #undef ctf_string
387 #define ctf_string(_item, _src)
388
389 #undef TP_PROTO
390 #define TP_PROTO(args...) args
391
392 #undef TP_FIELDS
393 #define TP_FIELDS(args...) args
394
395 #undef TRACEPOINT_EVENT_CLASS
396 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _proto, _args, _fields) \
397 static inline size_t __event_get_align__##_provider##___##_name(_proto) \
398 { \
399 size_t __event_align = 1; \
400 _fields \
401 return __event_align; \
402 }
403
404 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
405
406
407 /*
408 * Stage 7 of the trace events.
409 *
410 * Create the probe function : call even size calculation and write event data
411 * into the buffer.
412 *
413 * We use both the field and assignment macros to write the fields in the order
414 * defined in the field declaration. The field declarations control the
415 * execution order, jumping to the appropriate assignment block.
416 */
417
418 /* Reset all macros within TRACEPOINT_EVENT */
419 #include <lttng/ust-tracepoint-event-reset.h>
420
421 #undef ctf_integer_ext
422 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
423 { \
424 _type __tmp = (_src); \
425 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
426 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
427 }
428
429 #undef ctf_float
430 #define ctf_float(_type, _item, _src) \
431 { \
432 _type __tmp = (_src); \
433 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
434 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
435 }
436
437 #undef ctf_array_encoded
438 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
439 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
440 __chan->ops->event_write(&__ctx, _src, sizeof(_type) * (_length));
441
442 #undef ctf_sequence_encoded
443 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
444 _src_length, _encoding) \
445 { \
446 _length_type __tmpl = __dynamic_len[__dynamic_len_idx]; \
447 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_length_type));\
448 __chan->ops->event_write(&__ctx, &__tmpl, sizeof(_length_type));\
449 } \
450 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
451 __chan->ops->event_write(&__ctx, _src, \
452 sizeof(_type) * __get_dynamic_len(dest));
453
454 #undef ctf_string
455 #define ctf_string(_item, _src) \
456 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(*(_src))); \
457 __chan->ops->event_write(&__ctx, _src, __get_dynamic_len(dest));
458
459 /* Beware: this get len actually consumes the len value */
460 #undef __get_dynamic_len
461 #define __get_dynamic_len(field) __dynamic_len[__dynamic_len_idx++]
462
463 #undef TP_PROTO
464 #define TP_PROTO(args...) args
465
466 #undef TP_VARS
467 #define TP_VARS(args...) args
468
469 #undef TP_FIELDS
470 #define TP_FIELDS(args...) args
471
472 #undef TRACEPOINT_EVENT_CLASS
473 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _proto, _args, _fields) \
474 static void __event_probe__##_provider##___##_name(void *__data, _proto) \
475 { \
476 struct ltt_event *__event = __data; \
477 struct ltt_channel *__chan = __event->chan; \
478 struct lttng_ust_lib_ring_buffer_ctx __ctx; \
479 size_t __event_len, __event_align; \
480 size_t __dynamic_len_idx = 0; \
481 size_t __dynamic_len[_TP_ARRAY_SIZE(__event_fields___##_provider##___##_name)]; \
482 int __ret; \
483 \
484 if (0) \
485 (void) __dynamic_len_idx; /* don't warn if unused */ \
486 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->session->active))) \
487 return; \
488 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->enabled))) \
489 return; \
490 if (caa_unlikely(!CMM_ACCESS_ONCE(__event->enabled))) \
491 return; \
492 __event_len = __event_get_size__##_provider##___##_name(__dynamic_len, _args); \
493 __event_align = __event_get_align__##_provider##___##_name(_args); \
494 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
495 __event_align, -1, __chan->handle); \
496 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
497 if (__ret < 0) \
498 return; \
499 _fields \
500 __chan->ops->event_commit(&__ctx); \
501 }
502
503 #undef TRACEPOINT_EVENT_CLASS_NOARGS
504 #define TRACEPOINT_EVENT_CLASS_NOARGS(_provider, _name, _fields) \
505 static void __event_probe__##_provider##___##_name(void *__data) \
506 { \
507 struct ltt_event *__event = __data; \
508 struct ltt_channel *__chan = __event->chan; \
509 struct lttng_ust_lib_ring_buffer_ctx __ctx; \
510 size_t __event_len, __event_align; \
511 int __ret; \
512 \
513 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->session->active))) \
514 return; \
515 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->enabled))) \
516 return; \
517 if (caa_unlikely(!CMM_ACCESS_ONCE(__event->enabled))) \
518 return; \
519 __event_len = 0; \
520 __event_align = 1; \
521 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
522 __event_align, -1, __chan->handle); \
523 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
524 if (__ret < 0) \
525 return; \
526 _fields \
527 __chan->ops->event_commit(&__ctx); \
528 }
529
530 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
531
532 #undef __get_dynamic_len
533
534 /*
535 * Stage 8 of the trace events.
536 *
537 * Register/unregister probes at module load/unload.
538 */
539
540 /* Reset all macros within TRACEPOINT_EVENT */
541 #include <lttng/ust-tracepoint-event-reset.h>
542
543 #define TP_ID1(_token, _provider) _token##_provider
544 #define TP_ID(_token, _provider) TP_ID1(_token, _provider)
545
546 static void __attribute__((constructor))
547 TP_ID(__lttng_events_init__, TRACEPOINT_PROVIDER)(void)
548 {
549 int ret;
550
551 ret = ltt_probe_register(&TP_ID(__probe_desc___, TRACEPOINT_PROVIDER));
552 assert(!ret);
553 }
554
555 static void __attribute__((destructor))
556 TP_ID(__lttng_events_exit__, TRACEPOINT_PROVIDER)(void)
557 {
558 ltt_probe_unregister(&TP_ID(__probe_desc___, TRACEPOINT_PROVIDER));
559 }
560
561 #undef TP_ID1
562 #undef TP_ID
This page took 0.040811 seconds and 5 git commands to generate.