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