7890c247453e93c0c4b0ee9038ce6531b1a49757
[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 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <urcu/compiler.h>
26 #include <urcu/rculist.h>
27 #include <lttng/ust-events.h>
28 #include <lttng/ringbuffer-config.h>
29 #include <lttng/ust-compiler.h>
30 #include <lttng/tracepoint.h>
31 #include <byteswap.h>
32 #include <string.h>
33
34 #define __LTTNG_UST_NULL_STRING "(null)"
35
36 #undef tp_list_for_each_entry_rcu
37 #define tp_list_for_each_entry_rcu(pos, head, member) \
38 for (pos = cds_list_entry(tp_rcu_dereference_bp((head)->next), __typeof__(*pos), member); \
39 &pos->member != (head); \
40 pos = cds_list_entry(tp_rcu_dereference_bp(pos->member.next), __typeof__(*pos), member))
41
42 /*
43 * TRACEPOINT_EVENT_CLASS declares a class of tracepoints receiving the
44 * same arguments and having the same field layout.
45 *
46 * TRACEPOINT_EVENT_INSTANCE declares an instance of a tracepoint, with
47 * its own provider and name. It refers to a class (template).
48 *
49 * TRACEPOINT_EVENT declared both a class and an instance and does a
50 * direct mapping from the instance to the class.
51 */
52
53 #undef TRACEPOINT_EVENT
54 #define TRACEPOINT_EVENT(_provider, _name, _args, _fields) \
55 TRACEPOINT_EVENT_CLASS(_provider, _name, \
56 _TP_PARAMS(_args), \
57 _TP_PARAMS(_fields)) \
58 TRACEPOINT_EVENT_INSTANCE(_provider, _name, _name, \
59 _TP_PARAMS(_args))
60
61 /* Helpers */
62 #define _TP_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
63
64 #define _tp_max_t(type, x, y) \
65 ({ \
66 type __max1 = (x); \
67 type __max2 = (y); \
68 __max1 > __max2 ? __max1: __max2; \
69 })
70
71 /*
72 * Stage 0 of tracepoint event generation.
73 *
74 * Check that each TRACEPOINT_EVENT provider argument match the
75 * TRACEPOINT_PROVIDER by creating dummy callbacks.
76 */
77
78 /* Reset all macros within TRACEPOINT_EVENT */
79 #include <lttng/ust-tracepoint-event-reset.h>
80
81 static inline lttng_ust_notrace
82 void _TP_COMBINE_TOKENS(__tracepoint_provider_mismatch_, TRACEPOINT_PROVIDER)(void);
83 static inline
84 void _TP_COMBINE_TOKENS(__tracepoint_provider_mismatch_, TRACEPOINT_PROVIDER)(void)
85 {
86 }
87
88 #undef TRACEPOINT_EVENT_CLASS
89 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
90 __tracepoint_provider_mismatch_##_provider();
91
92 #undef TRACEPOINT_EVENT_INSTANCE
93 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
94 __tracepoint_provider_mismatch_##_provider();
95
96 static inline lttng_ust_notrace
97 void _TP_COMBINE_TOKENS(__tracepoint_provider_check_, TRACEPOINT_PROVIDER)(void);
98 static inline
99 void _TP_COMBINE_TOKENS(__tracepoint_provider_check_, TRACEPOINT_PROVIDER)(void)
100 {
101 #include TRACEPOINT_INCLUDE
102 }
103
104 /*
105 * Stage 0.1 of tracepoint event generation.
106 *
107 * Check that each TRACEPOINT_EVENT provider:name does not exceed the
108 * tracepoint name length limit.
109 */
110
111 /* Reset all macros within TRACEPOINT_EVENT */
112 #include <lttng/ust-tracepoint-event-reset.h>
113
114 #undef TRACEPOINT_EVENT_INSTANCE
115 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
116 static const char \
117 __tp_name_len_check##_provider##___##_name[LTTNG_UST_SYM_NAME_LEN] \
118 __attribute__((unused)) = \
119 #_provider ":" #_name;
120
121 #include TRACEPOINT_INCLUDE
122
123 /*
124 * Stage 0.2 of tracepoint event generation.
125 *
126 * Create dummy trace prototypes for each event class, and for each used
127 * template. This will allow checking whether the prototypes from the
128 * class and the instance using the class actually match.
129 */
130
131 /* Reset all macros within TRACEPOINT_EVENT */
132 #include <lttng/ust-tracepoint-event-reset.h>
133
134 #undef TP_ARGS
135 #define TP_ARGS(...) __VA_ARGS__
136
137 #undef TRACEPOINT_EVENT_INSTANCE
138 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
139 void __event_template_proto___##_provider##___##_template(_TP_ARGS_DATA_PROTO(_args));
140
141 #undef TRACEPOINT_EVENT_CLASS
142 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
143 void __event_template_proto___##_provider##___##_name(_TP_ARGS_DATA_PROTO(_args));
144
145 #include TRACEPOINT_INCLUDE
146
147 /*
148 * Stage 0.9 of tracepoint event generation
149 *
150 * Unfolding the enums
151 */
152 #include <lttng/ust-tracepoint-event-reset.h>
153
154 /* Enumeration entry (single value) */
155 #undef ctf_enum_value
156 #define ctf_enum_value(_string, _value) \
157 { \
158 .start = { \
159 .value = lttng_is_signed_type(__typeof__(_value)) ? \
160 (long long) (_value) : (_value), \
161 .signedness = lttng_is_signed_type(__typeof__(_value)), \
162 }, \
163 .end = { \
164 .value = lttng_is_signed_type(__typeof__(_value)) ? \
165 (long long) (_value) : (_value), \
166 .signedness = lttng_is_signed_type(__typeof__(_value)), \
167 }, \
168 .string = (_string), \
169 },
170
171 /* Enumeration entry (range) */
172 #undef ctf_enum_range
173 #define ctf_enum_range(_string, _range_start, _range_end) \
174 { \
175 .start = { \
176 .value = lttng_is_signed_type(__typeof__(_range_start)) ? \
177 (long long) (_range_start) : (_range_start), \
178 .signedness = lttng_is_signed_type(__typeof__(_range_start)), \
179 }, \
180 .end = { \
181 .value = lttng_is_signed_type(__typeof__(_range_end)) ? \
182 (long long) (_range_end) : (_range_end), \
183 .signedness = lttng_is_signed_type(__typeof__(_range_end)), \
184 }, \
185 .string = (_string), \
186 },
187
188 /* Enumeration entry (automatic value; follows the rules of CTF) */
189 #undef ctf_enum_auto
190 #define ctf_enum_auto(_string) \
191 { \
192 .start = { \
193 .value = -1ULL, \
194 .signedness = 0, \
195 }, \
196 .end = { \
197 .value = -1ULL, \
198 .signedness = 0, \
199 }, \
200 .string = (_string), \
201 .u = { \
202 .extra = { \
203 .options = LTTNG_ENUM_ENTRY_OPTION_IS_AUTO, \
204 }, \
205 }, \
206 },
207
208 #undef TP_ENUM_VALUES
209 #define TP_ENUM_VALUES(...) \
210 __VA_ARGS__
211
212 #undef TRACEPOINT_ENUM
213 #define TRACEPOINT_ENUM(_provider, _name, _values) \
214 const struct lttng_enum_entry __enum_values__##_provider##_##_name[] = { \
215 _values \
216 ctf_enum_value("", 0) /* Dummy, 0-len array forbidden by C99. */ \
217 };
218
219 #include TRACEPOINT_INCLUDE
220
221 /*
222 * Stage 1 of tracepoint event generation.
223 *
224 * Create event field type metadata section.
225 * Each event produce an array of fields.
226 */
227
228 /* Reset all macros within TRACEPOINT_EVENT */
229 #include <lttng/ust-tracepoint-event-reset.h>
230 #include <lttng/ust-tracepoint-event-write.h>
231 #include <lttng/ust-tracepoint-event-nowrite.h>
232
233 #undef _ctf_integer_ext
234 #define _ctf_integer_ext(_type, _item, _src, _byte_order, _base, _nowrite) \
235 { \
236 .name = #_item, \
237 .type = __type_integer(_type, _byte_order, _base, none),\
238 .nowrite = _nowrite, \
239 },
240
241 #undef _ctf_float
242 #define _ctf_float(_type, _item, _src, _nowrite) \
243 { \
244 .name = #_item, \
245 .type = __type_float(_type), \
246 .nowrite = _nowrite, \
247 },
248
249 #undef _ctf_array_encoded
250 #define _ctf_array_encoded(_type, _item, _src, _byte_order, \
251 _length, _encoding, _nowrite, \
252 _elem_type_base) \
253 { \
254 .name = #_item, \
255 .type = \
256 { \
257 .atype = atype_array, \
258 .u = \
259 { \
260 .array = \
261 { \
262 .elem_type = __type_integer(_type, _byte_order, _elem_type_base, _encoding), \
263 .length = _length, \
264 } \
265 } \
266 }, \
267 .nowrite = _nowrite, \
268 },
269
270 #undef _ctf_sequence_encoded
271 #define _ctf_sequence_encoded(_type, _item, _src, _byte_order, \
272 _length_type, _src_length, _encoding, _nowrite, \
273 _elem_type_base) \
274 { \
275 .name = #_item, \
276 .type = \
277 { \
278 .atype = atype_sequence, \
279 .u = \
280 { \
281 .sequence = \
282 { \
283 .length_type = __type_integer(_length_type, BYTE_ORDER, 10, none), \
284 .elem_type = __type_integer(_type, _byte_order, _elem_type_base, _encoding), \
285 }, \
286 }, \
287 }, \
288 .nowrite = _nowrite, \
289 },
290
291 #undef _ctf_string
292 #define _ctf_string(_item, _src, _nowrite) \
293 { \
294 .name = #_item, \
295 .type = \
296 { \
297 .atype = atype_string, \
298 .u = \
299 { \
300 .basic = { .string = { .encoding = lttng_encode_UTF8 } } \
301 }, \
302 }, \
303 .nowrite = _nowrite, \
304 },
305
306 #undef _ctf_enum
307 #define _ctf_enum(_provider, _name, _type, _item, _src, _nowrite) \
308 { \
309 .name = #_item, \
310 .type = { \
311 .atype = atype_enum, \
312 .u = { \
313 .basic = { \
314 .enumeration = { \
315 .desc = &__enum_##_provider##_##_name, \
316 .container_type = { \
317 .size = sizeof(_type) * CHAR_BIT, \
318 .alignment = lttng_alignof(_type) * CHAR_BIT, \
319 .signedness = lttng_is_signed_type(_type), \
320 .reverse_byte_order = 0, \
321 .base = 10, \
322 .encoding = lttng_encode_none, \
323 }, \
324 }, \
325 }, \
326 }, \
327 }, \
328 .nowrite = _nowrite, \
329 },
330
331 #undef TP_FIELDS
332 #define TP_FIELDS(...) __VA_ARGS__ /* Only one used in this phase */
333
334 #undef TRACEPOINT_EVENT_CLASS
335 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
336 static const struct lttng_event_field __event_fields___##_provider##___##_name[] = { \
337 _fields \
338 ctf_integer(int, dummy, 0) /* Dummy, C99 forbids 0-len array. */ \
339 };
340
341 #undef TRACEPOINT_ENUM
342 #define TRACEPOINT_ENUM(_provider, _name, _values) \
343 static const struct lttng_enum_desc __enum_##_provider##_##_name = { \
344 .name = #_provider "_" #_name, \
345 .entries = __enum_values__##_provider##_##_name, \
346 .nr_entries = _TP_ARRAY_SIZE(__enum_values__##_provider##_##_name) - 1, \
347 };
348
349 #include TRACEPOINT_INCLUDE
350
351 /*
352 * Stage 2 of tracepoint event generation.
353 *
354 * Create probe callback prototypes.
355 */
356
357 /* Reset all macros within TRACEPOINT_EVENT */
358 #include <lttng/ust-tracepoint-event-reset.h>
359
360 #undef TP_ARGS
361 #define TP_ARGS(...) __VA_ARGS__
362
363 #undef TRACEPOINT_EVENT_CLASS
364 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
365 static void __event_probe__##_provider##___##_name(_TP_ARGS_DATA_PROTO(_args));
366
367 #include TRACEPOINT_INCLUDE
368
369 /*
370 * Stage 3.0 of tracepoint event generation.
371 *
372 * Create static inline function that calculates event size.
373 */
374
375 /* Reset all macros within TRACEPOINT_EVENT */
376 #include <lttng/ust-tracepoint-event-reset.h>
377 #include <lttng/ust-tracepoint-event-write.h>
378
379 #undef _ctf_integer_ext
380 #define _ctf_integer_ext(_type, _item, _src, _byte_order, _base, _nowrite) \
381 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
382 __event_len += sizeof(_type);
383
384 #undef _ctf_float
385 #define _ctf_float(_type, _item, _src, _nowrite) \
386 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
387 __event_len += sizeof(_type);
388
389 #undef _ctf_array_encoded
390 #define _ctf_array_encoded(_type, _item, _src, _byte_order, _length, _encoding, \
391 _nowrite, _elem_type_base) \
392 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
393 __event_len += sizeof(_type) * (_length);
394
395 #undef _ctf_sequence_encoded
396 #define _ctf_sequence_encoded(_type, _item, _src, _byte_order, _length_type, \
397 _src_length, _encoding, _nowrite, _elem_type_base) \
398 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_length_type)); \
399 __event_len += sizeof(_length_type); \
400 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
401 __dynamic_len[__dynamic_len_idx] = (_src_length); \
402 __event_len += sizeof(_type) * __dynamic_len[__dynamic_len_idx]; \
403 __dynamic_len_idx++;
404
405 #undef _ctf_string
406 #define _ctf_string(_item, _src, _nowrite) \
407 __event_len += __dynamic_len[__dynamic_len_idx++] = \
408 strlen((_src) ? (_src) : __LTTNG_UST_NULL_STRING) + 1;
409
410 #undef _ctf_enum
411 #define _ctf_enum(_provider, _name, _type, _item, _src, _nowrite) \
412 _ctf_integer_ext(_type, _item, _src, BYTE_ORDER, 10, _nowrite)
413
414 #undef TP_ARGS
415 #define TP_ARGS(...) __VA_ARGS__
416
417 #undef TP_FIELDS
418 #define TP_FIELDS(...) __VA_ARGS__
419
420 #undef TRACEPOINT_EVENT_CLASS
421 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
422 static inline lttng_ust_notrace \
423 size_t __event_get_size__##_provider##___##_name(size_t *__dynamic_len, _TP_ARGS_DATA_PROTO(_args)); \
424 static inline \
425 size_t __event_get_size__##_provider##___##_name(size_t *__dynamic_len, _TP_ARGS_DATA_PROTO(_args)) \
426 { \
427 size_t __event_len = 0; \
428 unsigned int __dynamic_len_idx = 0; \
429 \
430 if (0) \
431 (void) __dynamic_len_idx; /* don't warn if unused */ \
432 _fields \
433 return __event_len; \
434 }
435
436 #include TRACEPOINT_INCLUDE
437
438 /*
439 * Stage 3.1 of tracepoint event generation.
440 *
441 * Create static inline function that layout the filter stack data.
442 * We make both write and nowrite data available to the filter.
443 */
444
445 /* Reset all macros within TRACEPOINT_EVENT */
446 #include <lttng/ust-tracepoint-event-reset.h>
447 #include <lttng/ust-tracepoint-event-write.h>
448 #include <lttng/ust-tracepoint-event-nowrite.h>
449
450 #undef _ctf_integer_ext
451 #define _ctf_integer_ext(_type, _item, _src, _byte_order, _base, _nowrite) \
452 if (lttng_is_signed_type(_type)) { \
453 int64_t __ctf_tmp_int64; \
454 switch (sizeof(_type)) { \
455 case 1: \
456 { \
457 union { _type t; int8_t v; } __tmp = { (_type) (_src) }; \
458 __ctf_tmp_int64 = (int64_t) __tmp.v; \
459 break; \
460 } \
461 case 2: \
462 { \
463 union { _type t; int16_t v; } __tmp = { (_type) (_src) }; \
464 if (_byte_order != BYTE_ORDER) \
465 __tmp.v = bswap_16(__tmp.v); \
466 __ctf_tmp_int64 = (int64_t) __tmp.v; \
467 break; \
468 } \
469 case 4: \
470 { \
471 union { _type t; int32_t v; } __tmp = { (_type) (_src) }; \
472 if (_byte_order != BYTE_ORDER) \
473 __tmp.v = bswap_32(__tmp.v); \
474 __ctf_tmp_int64 = (int64_t) __tmp.v; \
475 break; \
476 } \
477 case 8: \
478 { \
479 union { _type t; int64_t v; } __tmp = { (_type) (_src) }; \
480 if (_byte_order != BYTE_ORDER) \
481 __tmp.v = bswap_64(__tmp.v); \
482 __ctf_tmp_int64 = (int64_t) __tmp.v; \
483 break; \
484 } \
485 default: \
486 abort(); \
487 }; \
488 memcpy(__stack_data, &__ctf_tmp_int64, sizeof(int64_t)); \
489 } else { \
490 uint64_t __ctf_tmp_uint64; \
491 switch (sizeof(_type)) { \
492 case 1: \
493 { \
494 union { _type t; uint8_t v; } __tmp = { (_type) (_src) }; \
495 __ctf_tmp_uint64 = (uint64_t) __tmp.v; \
496 break; \
497 } \
498 case 2: \
499 { \
500 union { _type t; uint16_t v; } __tmp = { (_type) (_src) }; \
501 if (_byte_order != BYTE_ORDER) \
502 __tmp.v = bswap_16(__tmp.v); \
503 __ctf_tmp_uint64 = (uint64_t) __tmp.v; \
504 break; \
505 } \
506 case 4: \
507 { \
508 union { _type t; uint32_t v; } __tmp = { (_type) (_src) }; \
509 if (_byte_order != BYTE_ORDER) \
510 __tmp.v = bswap_32(__tmp.v); \
511 __ctf_tmp_uint64 = (uint64_t) __tmp.v; \
512 break; \
513 } \
514 case 8: \
515 { \
516 union { _type t; uint64_t v; } __tmp = { (_type) (_src) }; \
517 if (_byte_order != BYTE_ORDER) \
518 __tmp.v = bswap_64(__tmp.v); \
519 __ctf_tmp_uint64 = (uint64_t) __tmp.v; \
520 break; \
521 } \
522 default: \
523 abort(); \
524 }; \
525 memcpy(__stack_data, &__ctf_tmp_uint64, sizeof(uint64_t)); \
526 } \
527 __stack_data += sizeof(int64_t);
528
529 #undef _ctf_float
530 #define _ctf_float(_type, _item, _src, _nowrite) \
531 { \
532 double __ctf_tmp_double = (double) (_type) (_src); \
533 memcpy(__stack_data, &__ctf_tmp_double, sizeof(double)); \
534 __stack_data += sizeof(double); \
535 }
536
537 #undef _ctf_array_encoded
538 #define _ctf_array_encoded(_type, _item, _src, _byte_order, _length, \
539 _encoding, _nowrite, _elem_type_base) \
540 { \
541 unsigned long __ctf_tmp_ulong = (unsigned long) (_length); \
542 const void *__ctf_tmp_ptr = (_src); \
543 memcpy(__stack_data, &__ctf_tmp_ulong, sizeof(unsigned long)); \
544 __stack_data += sizeof(unsigned long); \
545 memcpy(__stack_data, &__ctf_tmp_ptr, sizeof(void *)); \
546 __stack_data += sizeof(void *); \
547 }
548
549 #undef _ctf_sequence_encoded
550 #define _ctf_sequence_encoded(_type, _item, _src, _byte_order, _length_type, \
551 _src_length, _encoding, _nowrite, _elem_type_base) \
552 { \
553 unsigned long __ctf_tmp_ulong = (unsigned long) (_src_length); \
554 const void *__ctf_tmp_ptr = (_src); \
555 memcpy(__stack_data, &__ctf_tmp_ulong, sizeof(unsigned long)); \
556 __stack_data += sizeof(unsigned long); \
557 memcpy(__stack_data, &__ctf_tmp_ptr, sizeof(void *)); \
558 __stack_data += sizeof(void *); \
559 }
560
561 #undef _ctf_string
562 #define _ctf_string(_item, _src, _nowrite) \
563 { \
564 const void *__ctf_tmp_ptr = \
565 ((_src) ? (_src) : __LTTNG_UST_NULL_STRING); \
566 memcpy(__stack_data, &__ctf_tmp_ptr, sizeof(void *)); \
567 __stack_data += sizeof(void *); \
568 }
569
570 #undef _ctf_enum
571 #define _ctf_enum(_provider, _name, _type, _item, _src, _nowrite) \
572 _ctf_integer_ext(_type, _item, _src, BYTE_ORDER, 10, _nowrite)
573
574 #undef TP_ARGS
575 #define TP_ARGS(...) __VA_ARGS__
576
577 #undef TP_FIELDS
578 #define TP_FIELDS(...) __VA_ARGS__
579
580 #undef TRACEPOINT_EVENT_CLASS
581 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
582 static inline \
583 void __event_prepare_filter_stack__##_provider##___##_name(char *__stack_data,\
584 _TP_ARGS_DATA_PROTO(_args)) \
585 { \
586 _fields \
587 }
588
589 #include TRACEPOINT_INCLUDE
590
591 /*
592 * Stage 4 of tracepoint event generation.
593 *
594 * Create static inline function that calculates event payload alignment.
595 */
596
597 /* Reset all macros within TRACEPOINT_EVENT */
598 #include <lttng/ust-tracepoint-event-reset.h>
599 #include <lttng/ust-tracepoint-event-write.h>
600
601 #undef _ctf_integer_ext
602 #define _ctf_integer_ext(_type, _item, _src, _byte_order, _base, _nowrite) \
603 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
604
605 #undef _ctf_float
606 #define _ctf_float(_type, _item, _src, _nowrite) \
607 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
608
609 #undef _ctf_array_encoded
610 #define _ctf_array_encoded(_type, _item, _src, _byte_order, _length, \
611 _encoding, _nowrite, _elem_type_base) \
612 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
613
614 #undef _ctf_sequence_encoded
615 #define _ctf_sequence_encoded(_type, _item, _src, _byte_order, _length_type, \
616 _src_length, _encoding, _nowrite, _elem_type_base) \
617 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_length_type)); \
618 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
619
620 #undef _ctf_string
621 #define _ctf_string(_item, _src, _nowrite)
622
623 #undef _ctf_enum
624 #define _ctf_enum(_provider, _name, _type, _item, _src, _nowrite) \
625 _ctf_integer_ext(_type, _item, _src, BYTE_ORDER, 10, _nowrite)
626
627 #undef TP_ARGS
628 #define TP_ARGS(...) __VA_ARGS__
629
630 #undef TP_FIELDS
631 #define TP_FIELDS(...) __VA_ARGS__
632
633 #undef TRACEPOINT_EVENT_CLASS
634 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
635 static inline lttng_ust_notrace \
636 size_t __event_get_align__##_provider##___##_name(_TP_ARGS_PROTO(_args)); \
637 static inline \
638 size_t __event_get_align__##_provider##___##_name(_TP_ARGS_PROTO(_args)) \
639 { \
640 size_t __event_align = 1; \
641 _fields \
642 return __event_align; \
643 }
644
645 #include TRACEPOINT_INCLUDE
646
647
648 /*
649 * Stage 5 of tracepoint event generation.
650 *
651 * Create the probe function. This function calls event size calculation
652 * and writes event data into the buffer.
653 */
654
655 /* Reset all macros within TRACEPOINT_EVENT */
656 #include <lttng/ust-tracepoint-event-reset.h>
657 #include <lttng/ust-tracepoint-event-write.h>
658
659 #undef _ctf_integer_ext
660 #define _ctf_integer_ext(_type, _item, _src, _byte_order, _base, _nowrite) \
661 { \
662 _type __tmp = (_src); \
663 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
664 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
665 }
666
667 #undef _ctf_float
668 #define _ctf_float(_type, _item, _src, _nowrite) \
669 { \
670 _type __tmp = (_src); \
671 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
672 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
673 }
674
675 #undef _ctf_array_encoded
676 #define _ctf_array_encoded(_type, _item, _src, _byte_order, _length, \
677 _encoding, _nowrite, _elem_type_base) \
678 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
679 __chan->ops->event_write(&__ctx, _src, sizeof(_type) * (_length));
680
681 #undef _ctf_sequence_encoded
682 #define _ctf_sequence_encoded(_type, _item, _src, _byte_order, _length_type, \
683 _src_length, _encoding, _nowrite, _elem_type_base) \
684 { \
685 _length_type __tmpl = __stackvar.__dynamic_len[__dynamic_len_idx]; \
686 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_length_type));\
687 __chan->ops->event_write(&__ctx, &__tmpl, sizeof(_length_type));\
688 } \
689 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
690 __chan->ops->event_write(&__ctx, _src, \
691 sizeof(_type) * __get_dynamic_len(dest));
692
693 /*
694 * __chan->ops->u.has_strcpy is a flag letting us know if the LTTng-UST
695 * tracepoint provider ABI implements event_strcpy. This dynamic check
696 * can be removed when the tracepoint provider ABI moves to 2.
697 */
698 #if (LTTNG_UST_PROVIDER_MAJOR > 1)
699 #error "Tracepoint probe provider major version has changed. Please remove dynamic check for has_strcpy."
700 #endif
701
702 #undef _ctf_string
703 #define _ctf_string(_item, _src, _nowrite) \
704 { \
705 const char *__ctf_tmp_string = \
706 ((_src) ? (_src) : __LTTNG_UST_NULL_STRING); \
707 lib_ring_buffer_align_ctx(&__ctx, \
708 lttng_alignof(*__ctf_tmp_string)); \
709 if (__chan->ops->u.has_strcpy) \
710 __chan->ops->event_strcpy(&__ctx, __ctf_tmp_string, \
711 __get_dynamic_len(dest)); \
712 else \
713 __chan->ops->event_write(&__ctx, __ctf_tmp_string, \
714 __get_dynamic_len(dest)); \
715 }
716
717
718 #undef _ctf_enum
719 #define _ctf_enum(_provider, _name, _type, _item, _src, _nowrite) \
720 _ctf_integer_ext(_type, _item, _src, BYTE_ORDER, 10, _nowrite)
721
722 /* Beware: this get len actually consumes the len value */
723 #undef __get_dynamic_len
724 #define __get_dynamic_len(field) __stackvar.__dynamic_len[__dynamic_len_idx++]
725
726 #undef TP_ARGS
727 #define TP_ARGS(...) __VA_ARGS__
728
729 #undef TP_FIELDS
730 #define TP_FIELDS(...) __VA_ARGS__
731
732 /*
733 * For state dump, check that "session" argument (mandatory) matches the
734 * session this event belongs to. Ensures that we write state dump data only
735 * into the started session, not into all sessions.
736 */
737 #undef _TP_SESSION_CHECK
738 #ifdef TP_SESSION_CHECK
739 #define _TP_SESSION_CHECK(session, csession) (session == csession)
740 #else /* TP_SESSION_CHECK */
741 #define _TP_SESSION_CHECK(session, csession) 1
742 #endif /* TP_SESSION_CHECK */
743
744 /*
745 * Use of __builtin_return_address(0) sometimes seems to cause stack
746 * corruption on 32-bit PowerPC. Disable this feature on that
747 * architecture for now by always using the NULL value for the ip
748 * context.
749 */
750 #undef _TP_IP_PARAM
751 #ifdef TP_IP_PARAM
752 #define _TP_IP_PARAM(x) (x)
753 #else /* TP_IP_PARAM */
754
755 #if defined(__PPC__) && !defined(__PPC64__)
756 #define _TP_IP_PARAM(x) NULL
757 #else /* #if defined(__PPC__) && !defined(__PPC64__) */
758 #define _TP_IP_PARAM(x) __builtin_return_address(0)
759 #endif /* #else #if defined(__PPC__) && !defined(__PPC64__) */
760
761 #endif /* TP_IP_PARAM */
762
763 /*
764 * Using twice size for filter stack data to hold size and pointer for
765 * each field (worse case). For integers, max size required is 64-bit.
766 * Same for double-precision floats. Those fit within
767 * 2*sizeof(unsigned long) for all supported architectures.
768 * Perform UNION (||) of filter runtime list.
769 */
770 #undef TRACEPOINT_EVENT_CLASS
771 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
772 static lttng_ust_notrace \
773 void __event_probe__##_provider##___##_name(_TP_ARGS_DATA_PROTO(_args)); \
774 static \
775 void __event_probe__##_provider##___##_name(_TP_ARGS_DATA_PROTO(_args)) \
776 { \
777 struct lttng_event *__event = (struct lttng_event *) __tp_data; \
778 struct lttng_channel *__chan = __event->chan; \
779 struct lttng_ust_lib_ring_buffer_ctx __ctx; \
780 struct lttng_stack_ctx __lttng_ctx; \
781 size_t __event_len, __event_align; \
782 size_t __dynamic_len_idx = 0; \
783 union { \
784 size_t __dynamic_len[_TP_ARRAY_SIZE(__event_fields___##_provider##___##_name) - 1]; \
785 char __filter_stack_data[2 * sizeof(unsigned long) * (_TP_ARRAY_SIZE(__event_fields___##_provider##___##_name) - 1)]; \
786 } __stackvar; \
787 int __ret; \
788 \
789 if (0) \
790 (void) __dynamic_len_idx; /* don't warn if unused */ \
791 if (!_TP_SESSION_CHECK(session, __chan->session)) \
792 return; \
793 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->session->active))) \
794 return; \
795 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->enabled))) \
796 return; \
797 if (caa_unlikely(!CMM_ACCESS_ONCE(__event->enabled))) \
798 return; \
799 if (caa_unlikely(!TP_RCU_LINK_TEST())) \
800 return; \
801 if (caa_unlikely(!cds_list_empty(&__event->bytecode_runtime_head))) { \
802 struct lttng_bytecode_runtime *bc_runtime; \
803 int __filter_record = __event->has_enablers_without_bytecode; \
804 \
805 __event_prepare_filter_stack__##_provider##___##_name(__stackvar.__filter_stack_data, \
806 _TP_ARGS_DATA_VAR(_args)); \
807 tp_list_for_each_entry_rcu(bc_runtime, &__event->bytecode_runtime_head, node) { \
808 if (caa_unlikely(bc_runtime->filter(bc_runtime, \
809 __stackvar.__filter_stack_data) & LTTNG_FILTER_RECORD_FLAG)) \
810 __filter_record = 1; \
811 } \
812 if (caa_likely(!__filter_record)) \
813 return; \
814 } \
815 __event_len = __event_get_size__##_provider##___##_name(__stackvar.__dynamic_len, \
816 _TP_ARGS_DATA_VAR(_args)); \
817 __event_align = __event_get_align__##_provider##___##_name(_TP_ARGS_VAR(_args)); \
818 memset(&__lttng_ctx, 0, sizeof(__lttng_ctx)); \
819 __lttng_ctx.event = __event; \
820 __lttng_ctx.chan_ctx = tp_rcu_dereference_bp(__chan->ctx); \
821 __lttng_ctx.event_ctx = tp_rcu_dereference_bp(__event->ctx); \
822 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
823 __event_align, -1, __chan->handle, &__lttng_ctx); \
824 __ctx.ip = _TP_IP_PARAM(TP_IP_PARAM); \
825 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
826 if (__ret < 0) \
827 return; \
828 _fields \
829 __chan->ops->event_commit(&__ctx); \
830 }
831
832 #include TRACEPOINT_INCLUDE
833
834 #undef __get_dynamic_len
835
836 /*
837 * Stage 5.1 of tracepoint event generation.
838 *
839 * Create probe signature
840 */
841
842 /* Reset all macros within TRACEPOINT_EVENT */
843 #include <lttng/ust-tracepoint-event-reset.h>
844
845 #undef TP_ARGS
846 #define TP_ARGS(...) __VA_ARGS__
847
848 #define _TP_EXTRACT_STRING2(...) #__VA_ARGS__
849
850 #undef TRACEPOINT_EVENT_CLASS
851 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
852 static const char __tp_event_signature___##_provider##___##_name[] = \
853 _TP_EXTRACT_STRING2(_args);
854
855 #include TRACEPOINT_INCLUDE
856
857 #undef _TP_EXTRACT_STRING2
858
859 /*
860 * Stage 6 of tracepoint event generation.
861 *
862 * Tracepoint loglevel mapping definition generation. We generate a
863 * symbol for each mapping for a provider/event to ensure at most a 1 to
864 * 1 mapping between events and loglevels. If the symbol is repeated,
865 * the compiler will complain.
866 */
867
868 /* Reset all macros within TRACEPOINT_EVENT */
869 #include <lttng/ust-tracepoint-event-reset.h>
870
871 /*
872 * Declare _loglevel___##__provider##___##__name as non-static, with
873 * hidden visibility for c++ handling of weakref. We do a weakref to the
874 * symbol in a later stage, which requires that the symbol is not
875 * mangled.
876 */
877 #ifdef __cplusplus
878 #define LTTNG_TP_EXTERN_C extern "C"
879 #else
880 #define LTTNG_TP_EXTERN_C
881 #endif
882
883 #undef TRACEPOINT_LOGLEVEL
884 #define TRACEPOINT_LOGLEVEL(__provider, __name, __loglevel) \
885 static const int _loglevel_value___##__provider##___##__name = __loglevel; \
886 LTTNG_TP_EXTERN_C const int *_loglevel___##__provider##___##__name \
887 __attribute__((visibility("hidden"))) = \
888 &_loglevel_value___##__provider##___##__name;
889
890 #include TRACEPOINT_INCLUDE
891
892 #undef LTTNG_TP_EXTERN_C
893
894 /*
895 * Stage 6.1 of tracepoint event generation.
896 *
897 * Tracepoint UML URI info.
898 */
899
900 /* Reset all macros within TRACEPOINT_EVENT */
901 #include <lttng/ust-tracepoint-event-reset.h>
902
903 /*
904 * Declare _model_emf_uri___##__provider##___##__name as non-static,
905 * with hidden visibility for c++ handling of weakref. We do a weakref
906 * to the symbol in a later stage, which requires that the symbol is not
907 * mangled.
908 */
909 #ifdef __cplusplus
910 #define LTTNG_TP_EXTERN_C extern "C"
911 #else
912 #define LTTNG_TP_EXTERN_C
913 #endif
914
915 #undef TRACEPOINT_MODEL_EMF_URI
916 #define TRACEPOINT_MODEL_EMF_URI(__provider, __name, __uri) \
917 LTTNG_TP_EXTERN_C const char *_model_emf_uri___##__provider##___##__name \
918 __attribute__((visibility("hidden"))) = __uri; \
919
920 #include TRACEPOINT_INCLUDE
921
922 #undef LTTNG_TP_EXTERN_C
923
924 /*
925 * Stage 7.1 of tracepoint event generation.
926 *
927 * Create events description structures. We use a weakref because
928 * loglevels are optional. If not declared, the event will point to the
929 * a loglevel that contains NULL.
930 */
931
932 /* Reset all macros within TRACEPOINT_EVENT */
933 #include <lttng/ust-tracepoint-event-reset.h>
934
935 #undef TRACEPOINT_EVENT_INSTANCE
936 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
937 static const int * \
938 __ref_loglevel___##_provider##___##_name \
939 __attribute__((weakref ("_loglevel___" #_provider "___" #_name))); \
940 static const char * \
941 __ref_model_emf_uri___##_provider##___##_name \
942 __attribute__((weakref ("_model_emf_uri___" #_provider "___" #_name)));\
943 static const struct lttng_event_desc __event_desc___##_provider##_##_name = { \
944 .name = #_provider ":" #_name, \
945 .probe_callback = (void (*)(void)) &__event_probe__##_provider##___##_template,\
946 .ctx = NULL, \
947 .fields = __event_fields___##_provider##___##_template, \
948 .nr_fields = _TP_ARRAY_SIZE(__event_fields___##_provider##___##_template) - 1, \
949 .loglevel = &__ref_loglevel___##_provider##___##_name, \
950 .signature = __tp_event_signature___##_provider##___##_template, \
951 .u = { \
952 .ext = { \
953 .model_emf_uri = &__ref_model_emf_uri___##_provider##___##_name, \
954 }, \
955 }, \
956 };
957
958 #include TRACEPOINT_INCLUDE
959
960 /*
961 * Stage 7.2 of tracepoint event generation.
962 *
963 * Create array of events.
964 */
965
966 /* Reset all macros within TRACEPOINT_EVENT */
967 #include <lttng/ust-tracepoint-event-reset.h>
968
969 #undef TRACEPOINT_EVENT_INSTANCE
970 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
971 &__event_desc___##_provider##_##_name,
972
973 static const struct lttng_event_desc *_TP_COMBINE_TOKENS(__event_desc___, TRACEPOINT_PROVIDER)[] = {
974 #include TRACEPOINT_INCLUDE
975 NULL, /* Dummy, C99 forbids 0-len array. */
976 };
977
978
979 /*
980 * Stage 8 of tracepoint event generation.
981 *
982 * Create a toplevel descriptor for the whole probe.
983 */
984
985 /* non-const because list head will be modified when registered. */
986 static struct lttng_probe_desc _TP_COMBINE_TOKENS(__probe_desc___, TRACEPOINT_PROVIDER) = {
987 .provider = __tp_stringify(TRACEPOINT_PROVIDER),
988 .event_desc = _TP_COMBINE_TOKENS(__event_desc___, TRACEPOINT_PROVIDER),
989 .nr_events = _TP_ARRAY_SIZE(_TP_COMBINE_TOKENS(__event_desc___, TRACEPOINT_PROVIDER)) - 1,
990 .head = { NULL, NULL },
991 .lazy_init_head = { NULL, NULL },
992 .lazy = 0,
993 .major = LTTNG_UST_PROVIDER_MAJOR,
994 .minor = LTTNG_UST_PROVIDER_MINOR,
995 };
996
997 static int _TP_COMBINE_TOKENS(__probe_register_refcount___, TRACEPOINT_PROVIDER);
998
999 /*
1000 * Stage 9 of tracepoint event generation.
1001 *
1002 * Register/unregister probes at module load/unload.
1003 *
1004 * Generate the constructor as an externally visible symbol for use when
1005 * linking the probe statically.
1006 *
1007 * Register refcount is protected by libc dynamic loader mutex.
1008 */
1009
1010 /* Reset all macros within TRACEPOINT_EVENT */
1011 #include <lttng/ust-tracepoint-event-reset.h>
1012 static void lttng_ust_notrace __attribute__((constructor))
1013 _TP_COMBINE_TOKENS(__lttng_events_init__, TRACEPOINT_PROVIDER)(void);
1014 static void
1015 _TP_COMBINE_TOKENS(__lttng_events_init__, TRACEPOINT_PROVIDER)(void)
1016 {
1017 int ret;
1018
1019 if (_TP_COMBINE_TOKENS(__probe_register_refcount___,
1020 TRACEPOINT_PROVIDER)++) {
1021 return;
1022 }
1023 /*
1024 * __tracepoint_provider_check_ ## TRACEPOINT_PROVIDER() is a
1025 * static inline function that ensures every probe PROVIDER
1026 * argument match the provider within which they appear. It
1027 * calls empty static inline functions, and therefore has no
1028 * runtime effect. However, if it detects an error, a linker
1029 * error will appear.
1030 */
1031 _TP_COMBINE_TOKENS(__tracepoint_provider_check_, TRACEPOINT_PROVIDER)();
1032 ret = lttng_probe_register(&_TP_COMBINE_TOKENS(__probe_desc___, TRACEPOINT_PROVIDER));
1033 if (ret) {
1034 fprintf(stderr, "LTTng-UST: Error (%d) while registering tracepoint probe.\n", ret);
1035 abort();
1036 }
1037 }
1038
1039 static void lttng_ust_notrace __attribute__((destructor))
1040 _TP_COMBINE_TOKENS(__lttng_events_exit__, TRACEPOINT_PROVIDER)(void);
1041 static void
1042 _TP_COMBINE_TOKENS(__lttng_events_exit__, TRACEPOINT_PROVIDER)(void)
1043 {
1044 if (--_TP_COMBINE_TOKENS(__probe_register_refcount___,
1045 TRACEPOINT_PROVIDER)) {
1046 return;
1047 }
1048 lttng_probe_unregister(&_TP_COMBINE_TOKENS(__probe_desc___, TRACEPOINT_PROVIDER));
1049 }
1050
1051 int _TP_COMBINE_TOKENS(__tracepoint_provider_, TRACEPOINT_PROVIDER);
This page took 0.0483 seconds and 3 git commands to generate.