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