Clarify probe registration documentation/errors
[lttng-ust.git] / include / lttng / ust-tracepoint-event.h
1 /*
2 * Copyright (c) 2011-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <urcu/compiler.h>
18 #include <lttng/ust-events.h>
19 #include <lttng/ringbuffer-config.h>
20 #include <string.h>
21
22 /*
23 * Macro declarations used for all stages.
24 */
25
26 #undef ctf_integer
27 #define ctf_integer(_type, _item, _src) \
28 ctf_integer_ext(_type, _item, _src, BYTE_ORDER, 10)
29
30 #undef ctf_integer_hex
31 #define ctf_integer_hex(_type, _item, _src) \
32 ctf_integer_ext(_type, _item, _src, BYTE_ORDER, 16)
33
34 #undef ctf_integer_network
35 #define ctf_integer_network(_type, _item, _src) \
36 ctf_integer_ext(_type, _item, _src, BIG_ENDIAN, 10)
37
38 #undef ctf_integer_network_hex
39 #define ctf_integer_network_hex(_type, _item, _src) \
40 ctf_integer_ext(_type, _item, _src, BIG_ENDIAN, 16)
41
42 /* ctf_float is redefined at each step */
43
44 #undef ctf_array
45 #define ctf_array(_type, _item, _src, _length) \
46 ctf_array_encoded(_type, _item, _src, _length, none)
47
48 #undef ctf_array_text
49 #define ctf_array_text(_type, _item, _src, _length) \
50 ctf_array_encoded(_type, _item, _src, _length, UTF8)
51
52 #undef ctf_sequence
53 #define ctf_sequence(_type, _item, _src, _length_type, _src_length) \
54 ctf_sequence_encoded(_type, _item, _src, \
55 _length_type, _src_length, none)
56
57 #undef ctf_sequence_text
58 #define ctf_sequence_text(_type, _item, _src, _length_type, _src_length) \
59 ctf_sequence_encoded(_type, _item, _src, \
60 _length_type, _src_length, UTF8)
61
62 /* ctf_string is redefined at each step */
63
64 /*
65 * TRACEPOINT_EVENT_CLASS declares a class of tracepoints receiving the
66 * same arguments and having the same field layout.
67 *
68 * TRACEPOINT_EVENT_INSTANCE declares an instance of a tracepoint, with
69 * its own provider and name. It refers to a class (template).
70 *
71 * TRACEPOINT_EVENT declared both a class and an instance and does a
72 * direct mapping from the instance to the class.
73 */
74
75 #undef TRACEPOINT_EVENT
76 #define TRACEPOINT_EVENT(_provider, _name, _args, _fields) \
77 TRACEPOINT_EVENT_CLASS(_provider, _name, \
78 _TP_PARAMS(_args), \
79 _TP_PARAMS(_fields)) \
80 TRACEPOINT_EVENT_INSTANCE(_provider, _name, _name, \
81 _TP_PARAMS(_args))
82
83 /* Helpers */
84 #define _TP_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
85
86 #define _tp_max_t(type, x, y) \
87 ({ \
88 type __max1 = (x); \
89 type __max2 = (y); \
90 __max1 > __max2 ? __max1: __max2; \
91 })
92
93 /*
94 * Stage 0 of tracepoint event generation.
95 *
96 * Check that each TRACEPOINT_EVENT provider argument match the
97 * TRACEPOINT_PROVIDER by creating dummy callbacks.
98 */
99
100 /* Reset all macros within TRACEPOINT_EVENT */
101 #include <lttng/ust-tracepoint-event-reset.h>
102
103 static inline
104 void _TP_COMBINE_TOKENS(__tracepoint_provider_mismatch_, TRACEPOINT_PROVIDER)(void)
105 {
106 }
107
108 #undef TRACEPOINT_EVENT_CLASS
109 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
110 __tracepoint_provider_mismatch_##_provider();
111
112 #undef TRACEPOINT_EVENT_INSTANCE
113 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
114 __tracepoint_provider_mismatch_##_provider();
115
116 static inline
117 void _TP_COMBINE_TOKENS(__tracepoint_provider_check_, TRACEPOINT_PROVIDER)(void)
118 {
119 #include TRACEPOINT_INCLUDE
120 }
121
122 /*
123 * Stage 0.1 of tracepoint event generation.
124 *
125 * Check that each TRACEPOINT_EVENT provider:name does not exceed the
126 * tracepoint name length limit.
127 */
128
129 /* Reset all macros within TRACEPOINT_EVENT */
130 #include <lttng/ust-tracepoint-event-reset.h>
131
132 #undef TRACEPOINT_EVENT_INSTANCE
133 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
134 static const char \
135 __tp_name_len_check##_provider##___##_name[LTTNG_UST_SYM_NAME_LEN] \
136 __attribute__((unused)) = \
137 #_provider ":" #_name;
138
139 #include TRACEPOINT_INCLUDE
140
141 /*
142 * Stage 1 of tracepoint event generation.
143 *
144 * Create event field type metadata section.
145 * Each event produce an array of fields.
146 */
147
148 /* Reset all macros within TRACEPOINT_EVENT */
149 #include <lttng/ust-tracepoint-event-reset.h>
150
151 #undef ctf_integer_ext
152 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
153 { \
154 .name = #_item, \
155 .type = __type_integer(_type, _byte_order, _base, none),\
156 },
157
158 #undef ctf_float
159 #define ctf_float(_type, _item, _src) \
160 { \
161 .name = #_item, \
162 .type = __type_float(_type), \
163 },
164
165 #undef ctf_array_encoded
166 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
167 { \
168 .name = #_item, \
169 .type = \
170 { \
171 .atype = atype_array, \
172 .u.array = \
173 { \
174 .length = _length, \
175 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
176 }, \
177 }, \
178 },
179
180 #undef ctf_sequence_encoded
181 #define ctf_sequence_encoded(_type, _item, _src, \
182 _length_type, _src_length, _encoding) \
183 { \
184 .name = #_item, \
185 .type = \
186 { \
187 .atype = atype_sequence, \
188 .u.sequence = \
189 { \
190 .length_type = __type_integer(_length_type, BYTE_ORDER, 10, none), \
191 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
192 }, \
193 }, \
194 },
195
196 #undef ctf_string
197 #define ctf_string(_item, _src) \
198 { \
199 .name = #_item, \
200 .type = \
201 { \
202 .atype = atype_string, \
203 .u.basic.string.encoding = lttng_encode_UTF8, \
204 }, \
205 },
206
207 #undef TP_FIELDS
208 #define TP_FIELDS(...) __VA_ARGS__ /* Only one used in this phase */
209
210 #undef TRACEPOINT_EVENT_CLASS
211 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
212 static const struct lttng_event_field __event_fields___##_provider##___##_name[] = { \
213 _fields \
214 };
215
216 #include TRACEPOINT_INCLUDE
217
218 /*
219 * Stage 2 of tracepoint event generation.
220 *
221 * Create probe callback prototypes.
222 */
223
224 /* Reset all macros within TRACEPOINT_EVENT */
225 #include <lttng/ust-tracepoint-event-reset.h>
226
227 #undef TP_ARGS
228 #define TP_ARGS(...) __VA_ARGS__
229
230 #undef TRACEPOINT_EVENT_CLASS
231 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
232 static void __event_probe__##_provider##___##_name(_TP_ARGS_DATA_PROTO(_args));
233
234 #include TRACEPOINT_INCLUDE
235
236 /*
237 * Stage 3 of tracepoint event generation.
238 *
239 * Create static inline function that calculates event size.
240 */
241
242 /* Reset all macros within TRACEPOINT_EVENT */
243 #include <lttng/ust-tracepoint-event-reset.h>
244
245 #undef ctf_integer_ext
246 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
247 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
248 __event_len += sizeof(_type);
249
250 #undef ctf_float
251 #define ctf_float(_type, _item, _src) \
252 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
253 __event_len += sizeof(_type);
254
255 #undef ctf_array_encoded
256 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
257 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
258 __event_len += sizeof(_type) * (_length);
259
260 #undef ctf_sequence_encoded
261 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
262 _src_length, _encoding) \
263 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_length_type)); \
264 __event_len += sizeof(_length_type); \
265 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
266 __dynamic_len[__dynamic_len_idx] = (_src_length); \
267 __event_len += sizeof(_type) * __dynamic_len[__dynamic_len_idx]; \
268 __dynamic_len_idx++;
269
270 #undef ctf_string
271 #define ctf_string(_item, _src) \
272 __event_len += __dynamic_len[__dynamic_len_idx++] = strlen(_src) + 1;
273
274 #undef TP_ARGS
275 #define TP_ARGS(...) __VA_ARGS__
276
277 #undef TP_FIELDS
278 #define TP_FIELDS(...) __VA_ARGS__
279
280 #undef TRACEPOINT_EVENT_CLASS
281 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
282 static inline size_t __event_get_size__##_provider##___##_name(size_t *__dynamic_len, _TP_ARGS_DATA_PROTO(_args)) \
283 { \
284 size_t __event_len = 0; \
285 unsigned int __dynamic_len_idx = 0; \
286 \
287 if (0) \
288 (void) __dynamic_len_idx; /* don't warn if unused */ \
289 _fields \
290 return __event_len; \
291 }
292
293 #include TRACEPOINT_INCLUDE
294
295 /*
296 * Stage 4 of tracepoint event generation.
297 *
298 * Create static inline function that calculates event payload alignment.
299 */
300
301 /* Reset all macros within TRACEPOINT_EVENT */
302 #include <lttng/ust-tracepoint-event-reset.h>
303
304 #undef ctf_integer_ext
305 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
306 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
307
308 #undef ctf_float
309 #define ctf_float(_type, _item, _src) \
310 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
311
312 #undef ctf_array_encoded
313 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
314 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
315
316 #undef ctf_sequence_encoded
317 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
318 _src_length, _encoding) \
319 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_length_type)); \
320 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
321
322 #undef ctf_string
323 #define ctf_string(_item, _src)
324
325 #undef TP_ARGS
326 #define TP_ARGS(...) __VA_ARGS__
327
328 #undef TP_FIELDS
329 #define TP_FIELDS(...) __VA_ARGS__
330
331 #undef TRACEPOINT_EVENT_CLASS
332 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
333 static inline \
334 size_t __event_get_align__##_provider##___##_name(_TP_ARGS_PROTO(_args)) \
335 { \
336 size_t __event_align = 1; \
337 _fields \
338 return __event_align; \
339 }
340
341 #include TRACEPOINT_INCLUDE
342
343
344 /*
345 * Stage 5 of tracepoint event generation.
346 *
347 * Create the probe function. This function calls event size calculation
348 * and writes event data into the buffer.
349 */
350
351 /* Reset all macros within TRACEPOINT_EVENT */
352 #include <lttng/ust-tracepoint-event-reset.h>
353
354 #undef ctf_integer_ext
355 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
356 { \
357 _type __tmp = (_src); \
358 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
359 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
360 }
361
362 #undef ctf_float
363 #define ctf_float(_type, _item, _src) \
364 { \
365 _type __tmp = (_src); \
366 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
367 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
368 }
369
370 #undef ctf_array_encoded
371 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
372 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
373 __chan->ops->event_write(&__ctx, _src, sizeof(_type) * (_length));
374
375 #undef ctf_sequence_encoded
376 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
377 _src_length, _encoding) \
378 { \
379 _length_type __tmpl = __dynamic_len[__dynamic_len_idx]; \
380 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_length_type));\
381 __chan->ops->event_write(&__ctx, &__tmpl, sizeof(_length_type));\
382 } \
383 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
384 __chan->ops->event_write(&__ctx, _src, \
385 sizeof(_type) * __get_dynamic_len(dest));
386
387 #undef ctf_string
388 #define ctf_string(_item, _src) \
389 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(*(_src))); \
390 __chan->ops->event_write(&__ctx, _src, __get_dynamic_len(dest));
391
392 /* Beware: this get len actually consumes the len value */
393 #undef __get_dynamic_len
394 #define __get_dynamic_len(field) __dynamic_len[__dynamic_len_idx++]
395
396 #undef TP_ARGS
397 #define TP_ARGS(...) __VA_ARGS__
398
399 #undef TP_FIELDS
400 #define TP_FIELDS(...) __VA_ARGS__
401
402 #undef TRACEPOINT_EVENT_CLASS
403 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
404 static void __event_probe__##_provider##___##_name(_TP_ARGS_DATA_PROTO(_args))\
405 { \
406 struct ltt_event *__event = __tp_data; \
407 struct ltt_channel *__chan = __event->chan; \
408 struct lttng_ust_lib_ring_buffer_ctx __ctx; \
409 size_t __event_len, __event_align; \
410 size_t __dynamic_len_idx = 0; \
411 size_t __dynamic_len[_TP_ARRAY_SIZE(__event_fields___##_provider##___##_name)]; \
412 int __ret; \
413 \
414 if (0) \
415 (void) __dynamic_len_idx; /* don't warn if unused */ \
416 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->session->active))) \
417 return; \
418 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->enabled))) \
419 return; \
420 if (caa_unlikely(!CMM_ACCESS_ONCE(__event->enabled))) \
421 return; \
422 __event_len = __event_get_size__##_provider##___##_name(__dynamic_len,\
423 _TP_ARGS_DATA_VAR(_args)); \
424 __event_align = __event_get_align__##_provider##___##_name(_TP_ARGS_VAR(_args)); \
425 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
426 __event_align, -1, __chan->handle); \
427 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
428 if (__ret < 0) \
429 return; \
430 _fields \
431 __chan->ops->event_commit(&__ctx); \
432 }
433
434 #include TRACEPOINT_INCLUDE
435
436 #undef __get_dynamic_len
437
438 /*
439 * Stage 5.1 of tracepoint event generation.
440 *
441 * Create probe signature
442 */
443
444 /* Reset all macros within TRACEPOINT_EVENT */
445 #include <lttng/ust-tracepoint-event-reset.h>
446
447 #undef TP_ARGS
448 #define TP_ARGS(...) __VA_ARGS__
449
450 #define _TP_EXTRACT_STRING2(...) #__VA_ARGS__
451
452 #undef TRACEPOINT_EVENT_CLASS
453 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
454 const char __tp_event_signature___##_provider##___##_name[] = \
455 _TP_EXTRACT_STRING2(_args);
456
457 #include TRACEPOINT_INCLUDE
458
459 #undef _TP_EXTRACT_STRING2
460
461 /*
462 * Stage 6 of tracepoint event generation.
463 *
464 * Tracepoint loglevel mapping definition generation. We generate a
465 * symbol for each mapping for a provider/event to ensure at most a 1 to
466 * 1 mapping between events and loglevels. If the symbol is repeated,
467 * the compiler will complain.
468 */
469
470 /* Reset all macros within TRACEPOINT_EVENT */
471 #include <lttng/ust-tracepoint-event-reset.h>
472
473 #undef TRACEPOINT_LOGLEVEL
474 #define TRACEPOINT_LOGLEVEL(__provider, __name, __loglevel) \
475 static const int _loglevel_value___##__provider##___##__name = __loglevel; \
476 static const int *_loglevel___##__provider##___##__name = \
477 &_loglevel_value___##__provider##___##__name;
478
479 #include TRACEPOINT_INCLUDE
480
481 /*
482 * Stage 7.1 of tracepoint event generation.
483 *
484 * Create events description structures. We use a weakref because
485 * loglevels are optional. If not declared, the event will point to the
486 * a loglevel that contains NULL.
487 */
488
489 /* Reset all macros within TRACEPOINT_EVENT */
490 #include <lttng/ust-tracepoint-event-reset.h>
491
492 #undef TRACEPOINT_EVENT_INSTANCE
493 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
494 static const int * \
495 __ref_loglevel___##_provider##___##_name \
496 __attribute__((weakref ("_loglevel___" #_provider "___" #_name))); \
497 const struct lttng_event_desc __event_desc___##_provider##_##_name = { \
498 .fields = __event_fields___##_provider##___##_template, \
499 .name = #_provider ":" #_name, \
500 .probe_callback = (void (*)(void)) &__event_probe__##_provider##___##_template,\
501 .nr_fields = _TP_ARRAY_SIZE(__event_fields___##_provider##___##_template), \
502 .loglevel = &__ref_loglevel___##_provider##___##_name, \
503 .signature = __tp_event_signature___##_provider##___##_template, \
504 };
505
506 #include TRACEPOINT_INCLUDE
507
508 /*
509 * Stage 7.2 of tracepoint event generation.
510 *
511 * Create array of events.
512 */
513
514 /* Reset all macros within TRACEPOINT_EVENT */
515 #include <lttng/ust-tracepoint-event-reset.h>
516
517 #undef TRACEPOINT_EVENT_INSTANCE
518 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
519 &__event_desc___##_provider##_##_name,
520
521 static const struct lttng_event_desc *_TP_COMBINE_TOKENS(__event_desc___, TRACEPOINT_PROVIDER)[] = {
522 #include TRACEPOINT_INCLUDE
523 };
524
525
526 /*
527 * Stage 8 of tracepoint event generation.
528 *
529 * Create a toplevel descriptor for the whole probe.
530 */
531
532 /* non-const because list head will be modified when registered. */
533 static struct lttng_probe_desc _TP_COMBINE_TOKENS(__probe_desc___, TRACEPOINT_PROVIDER) = {
534 .provider = __tp_stringify(TRACEPOINT_PROVIDER),
535 .event_desc = _TP_COMBINE_TOKENS(__event_desc___, TRACEPOINT_PROVIDER),
536 .nr_events = _TP_ARRAY_SIZE(_TP_COMBINE_TOKENS(__event_desc___, TRACEPOINT_PROVIDER)),
537 };
538
539 /*
540 * Stage 9 of tracepoint event generation.
541 *
542 * Register/unregister probes at module load/unload.
543 *
544 * Generate the constructor as an externally visible symbol for use when
545 * linking the probe statically.
546 */
547
548 /* Reset all macros within TRACEPOINT_EVENT */
549 #include <lttng/ust-tracepoint-event-reset.h>
550 static void __attribute__((constructor))
551 _TP_COMBINE_TOKENS(__lttng_events_init__, TRACEPOINT_PROVIDER)(void)
552 {
553 int ret;
554
555 /*
556 * __tracepoint_provider_check_ ## TRACEPOINT_PROVIDER() is a
557 * static inline function that ensures every probe PROVIDER
558 * argument match the provider within which they appear. It
559 * calls empty static inline functions, and therefore has no
560 * runtime effect. However, if it detects an error, a linker
561 * error will appear.
562 */
563 _TP_COMBINE_TOKENS(__tracepoint_provider_check_, TRACEPOINT_PROVIDER)();
564 if (ret) {
565 fprintf(stderr, "LTTng-UST: Error (%d) while registering tracepoint probe. Duplicate registration of tracepoint probes having the same name is not allowed.\n", ret);
566 abort();
567 }
568 }
569
570 static void __attribute__((destructor))
571 _TP_COMBINE_TOKENS(__lttng_events_exit__, TRACEPOINT_PROVIDER)(void)
572 {
573 ltt_probe_unregister(&_TP_COMBINE_TOKENS(__probe_desc___, TRACEPOINT_PROVIDER));
574 }
575
576 int _TP_COMBINE_TOKENS(__tracepoint_provider_, TRACEPOINT_PROVIDER);
This page took 0.046982 seconds and 4 git commands to generate.