Tracepoint API change: combine TP_PROTO and TP_VARS into TP_ARGS, remove _NOARGS...
[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, args, fields) \
81 TRACEPOINT_EVENT_CLASS(provider, name, \
82 TP_PARAMS(args), \
83 TP_PARAMS(fields)) \
84 TRACEPOINT_EVENT_INSTANCE(provider, name, name, TP_PARAMS(args))
85
86 /* Helpers */
87 #define _TP_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
88
89 #define _tp_max_t(type, x, y) \
90 ({ \
91 type __max1 = (x); \
92 type __max2 = (y); \
93 __max1 > __max2 ? __max1: __max2; \
94 })
95
96 /*
97 * Stage 0 of the trace events.
98 *
99 * Check that each TRACEPOINT_EVENT provider argument match the
100 * TRACEPOINT_PROVIDER by creating dummy callbacks.
101 */
102
103 /* Reset all macros within TRACEPOINT_EVENT */
104 #include <lttng/ust-tracepoint-event-reset.h>
105
106 #define TP_ID1(_token, _provider) _token##_provider
107 #define TP_ID(_token, _provider) TP_ID1(_token, _provider)
108
109 static inline
110 void TP_ID(__tracepoint_provider_mismatch_, TRACEPOINT_PROVIDER)(void)
111 {
112 }
113
114 #undef TRACEPOINT_EVENT_CLASS
115 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
116 __tracepoint_provider_mismatch_##_provider();
117
118 #undef TRACEPOINT_EVENT_INSTANCE
119 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
120 __tracepoint_provider_mismatch_##_provider();
121
122 static __attribute__((unused))
123 void TP_ID(__tracepoint_provider_check_, TRACEPOINT_PROVIDER)(void)
124 {
125 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
126 }
127
128 #undef TP_ID1
129 #undef TP_ID
130
131 /*
132 * Stage 1 of the trace events.
133 *
134 * Create event field type metadata section.
135 * Each event produce an array of fields.
136 */
137
138 /* Reset all macros within TRACEPOINT_EVENT */
139 #include <lttng/ust-tracepoint-event-reset.h>
140
141 #undef ctf_integer_ext
142 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
143 { \
144 .name = #_item, \
145 .type = __type_integer(_type, _byte_order, _base, none),\
146 },
147
148 #undef ctf_float
149 #define ctf_float(_type, _item, _src) \
150 { \
151 .name = #_item, \
152 .type = __type_float(_type), \
153 },
154
155 #undef ctf_array_encoded
156 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
157 { \
158 .name = #_item, \
159 .type = \
160 { \
161 .atype = atype_array, \
162 .u.array = \
163 { \
164 .length = _length, \
165 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
166 }, \
167 }, \
168 },
169
170 #undef ctf_sequence_encoded
171 #define ctf_sequence_encoded(_type, _item, _src, \
172 _length_type, _src_length, _encoding) \
173 { \
174 .name = #_item, \
175 .type = \
176 { \
177 .atype = atype_sequence, \
178 .u.sequence = \
179 { \
180 .length_type = __type_integer(_length_type, BYTE_ORDER, 10, none), \
181 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
182 }, \
183 }, \
184 },
185
186 #undef ctf_string
187 #define ctf_string(_item, _src) \
188 { \
189 .name = #_item, \
190 .type = \
191 { \
192 .atype = atype_string, \
193 .u.basic.string.encoding = lttng_encode_UTF8, \
194 }, \
195 },
196
197 #undef TP_FIELDS
198 #define TP_FIELDS(args...) args /* Only one used in this phase */
199
200 #undef TRACEPOINT_EVENT_CLASS
201 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
202 static const struct lttng_event_field __event_fields___##_provider##___##_name[] = { \
203 _fields \
204 };
205
206 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
207
208 /*
209 * Stage 2 of the trace events.
210 *
211 * Create probe callback prototypes.
212 */
213
214 /* Reset all macros within TRACEPOINT_EVENT */
215 #include <lttng/ust-tracepoint-event-reset.h>
216
217 #undef TP_ARGS
218 #define TP_ARGS(args...) args
219
220 #undef TRACEPOINT_EVENT_CLASS
221 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
222 static void __event_probe__##_provider##___##_name(_TP_ARGS_PROTO_DATA(_args));
223
224 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
225
226 /*
227 * Stage 3 of the trace events.
228 *
229 * Create an array of events.
230 */
231
232 /* Reset all macros within TRACEPOINT_EVENT */
233 #include <lttng/ust-tracepoint-event-reset.h>
234
235 #undef TRACEPOINT_EVENT_INSTANCE
236 #define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
237 { \
238 .fields = __event_fields___##_provider##___##_template,\
239 .name = #_provider ":" #_name, \
240 .probe_callback = (void *) &__event_probe__##_provider##___##_template,\
241 .nr_fields = _TP_ARRAY_SIZE(__event_fields___##_provider##___##_template), \
242 },
243
244 #define TP_ID1(_token, _provider) _token##_provider
245 #define TP_ID(_token, _provider) TP_ID1(_token, _provider)
246
247 static const struct lttng_event_desc TP_ID(__event_desc___, TRACEPOINT_PROVIDER)[] = {
248 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
249 };
250
251 #undef TP_ID1
252 #undef TP_ID
253
254
255 /*
256 * Stage 4 of the trace events.
257 *
258 * Create a toplevel descriptor for the whole probe.
259 */
260
261 #define TP_ID1(_token, _provider) _token##_provider
262 #define TP_ID(_token, _provider) TP_ID1(_token, _provider)
263
264 /* non-const because list head will be modified when registered. */
265 static struct lttng_probe_desc TP_ID(__probe_desc___, TRACEPOINT_PROVIDER) = {
266 .event_desc = TP_ID(__event_desc___, TRACEPOINT_PROVIDER),
267 .nr_events = _TP_ARRAY_SIZE(TP_ID(__event_desc___, TRACEPOINT_PROVIDER)),
268 };
269
270 #undef TP_ID1
271 #undef TP_ID
272
273 /*
274 * Stage 5 of the trace events.
275 *
276 * Create static inline function that calculates event size.
277 */
278
279 /* Reset all macros within TRACEPOINT_EVENT */
280 #include <lttng/ust-tracepoint-event-reset.h>
281
282 #undef ctf_integer_ext
283 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
284 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
285 __event_len += sizeof(_type);
286
287 #undef ctf_float
288 #define ctf_float(_type, _item, _src) \
289 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
290 __event_len += sizeof(_type);
291
292 #undef ctf_array_encoded
293 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
294 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
295 __event_len += sizeof(_type) * (_length);
296
297 #undef ctf_sequence_encoded
298 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
299 _src_length, _encoding) \
300 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_length_type)); \
301 __event_len += sizeof(_length_type); \
302 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
303 __dynamic_len[__dynamic_len_idx] = (_src_length); \
304 __event_len += sizeof(_type) * __dynamic_len[__dynamic_len_idx]; \
305 __dynamic_len_idx++;
306
307 #undef ctf_string
308 #define ctf_string(_item, _src) \
309 __event_len += __dynamic_len[__dynamic_len_idx++] = strlen(_src) + 1;
310
311 #undef TP_ARGS
312 #define TP_ARGS(args...) args
313
314 #undef TP_FIELDS
315 #define TP_FIELDS(args...) args
316
317 #undef TRACEPOINT_EVENT_CLASS
318 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
319 static inline size_t __event_get_size__##_provider##___##_name(size_t *__dynamic_len, _TP_ARGS_PROTO_DATA(_args)) \
320 { \
321 size_t __event_len = 0; \
322 unsigned int __dynamic_len_idx = 0; \
323 \
324 if (0) \
325 (void) __dynamic_len_idx; /* don't warn if unused */ \
326 _fields \
327 return __event_len; \
328 }
329
330 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
331
332 /*
333 * Stage 6 of the trace events.
334 *
335 * Create static inline function that calculates event payload alignment.
336 */
337
338 /* Reset all macros within TRACEPOINT_EVENT */
339 #include <lttng/ust-tracepoint-event-reset.h>
340
341 #undef ctf_integer_ext
342 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
343 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
344
345 #undef ctf_float
346 #define ctf_float(_type, _item, _src) \
347 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
348
349 #undef ctf_array_encoded
350 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
351 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
352
353 #undef ctf_sequence_encoded
354 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
355 _src_length, _encoding) \
356 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_length_type)); \
357 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
358
359 #undef ctf_string
360 #define ctf_string(_item, _src)
361
362 #undef TP_ARGS
363 #define TP_ARGS(args...) args
364
365 #undef TP_FIELDS
366 #define TP_FIELDS(args...) args
367
368 #undef TRACEPOINT_EVENT_CLASS
369 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
370 static inline \
371 size_t __event_get_align__##_provider##___##_name(_TP_ARGS_PROTO(_args)) \
372 { \
373 size_t __event_align = 1; \
374 _fields \
375 return __event_align; \
376 }
377
378 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
379
380
381 /*
382 * Stage 7 of the trace events.
383 *
384 * Create the probe function : call even size calculation and write event data
385 * into the buffer.
386 *
387 * We use both the field and assignment macros to write the fields in the order
388 * defined in the field declaration. The field declarations control the
389 * execution order, jumping to the appropriate assignment block.
390 */
391
392 /* Reset all macros within TRACEPOINT_EVENT */
393 #include <lttng/ust-tracepoint-event-reset.h>
394
395 #undef ctf_integer_ext
396 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
397 { \
398 _type __tmp = (_src); \
399 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
400 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
401 }
402
403 #undef ctf_float
404 #define ctf_float(_type, _item, _src) \
405 { \
406 _type __tmp = (_src); \
407 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
408 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
409 }
410
411 #undef ctf_array_encoded
412 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
413 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
414 __chan->ops->event_write(&__ctx, _src, sizeof(_type) * (_length));
415
416 #undef ctf_sequence_encoded
417 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
418 _src_length, _encoding) \
419 { \
420 _length_type __tmpl = __dynamic_len[__dynamic_len_idx]; \
421 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_length_type));\
422 __chan->ops->event_write(&__ctx, &__tmpl, sizeof(_length_type));\
423 } \
424 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
425 __chan->ops->event_write(&__ctx, _src, \
426 sizeof(_type) * __get_dynamic_len(dest));
427
428 #undef ctf_string
429 #define ctf_string(_item, _src) \
430 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(*(_src))); \
431 __chan->ops->event_write(&__ctx, _src, __get_dynamic_len(dest));
432
433 /* Beware: this get len actually consumes the len value */
434 #undef __get_dynamic_len
435 #define __get_dynamic_len(field) __dynamic_len[__dynamic_len_idx++]
436
437 #undef TP_ARGS
438 #define TP_ARGS(args...) args
439
440 #undef TP_FIELDS
441 #define TP_FIELDS(args...) args
442
443 #undef TRACEPOINT_EVENT_CLASS
444 #define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
445 static void __event_probe__##_provider##___##_name(_TP_ARGS_PROTO_DATA(_args))\
446 { \
447 struct ltt_event *__event = __tp_cb_data; \
448 struct ltt_channel *__chan = __event->chan; \
449 struct lttng_ust_lib_ring_buffer_ctx __ctx; \
450 size_t __event_len, __event_align; \
451 size_t __dynamic_len_idx = 0; \
452 size_t __dynamic_len[_TP_ARRAY_SIZE(__event_fields___##_provider##___##_name)]; \
453 int __ret; \
454 \
455 if (0) \
456 (void) __dynamic_len_idx; /* don't warn if unused */ \
457 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->session->active))) \
458 return; \
459 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->enabled))) \
460 return; \
461 if (caa_unlikely(!CMM_ACCESS_ONCE(__event->enabled))) \
462 return; \
463 __event_len = __event_get_size__##_provider##___##_name(__dynamic_len,\
464 _TP_ARGS_VARS_DATA(_args)); \
465 __event_align = __event_get_align__##_provider##___##_name(_TP_ARGS_VARS(_args)); \
466 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
467 __event_align, -1, __chan->handle); \
468 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
469 if (__ret < 0) \
470 return; \
471 _fields \
472 __chan->ops->event_commit(&__ctx); \
473 }
474
475 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
476
477 #undef __get_dynamic_len
478
479 /*
480 * Stage 8 of the trace events.
481 *
482 * Register/unregister probes at module load/unload.
483 */
484
485 /* Reset all macros within TRACEPOINT_EVENT */
486 #include <lttng/ust-tracepoint-event-reset.h>
487
488 #define TP_ID1(_token, _provider) _token##_provider
489 #define TP_ID(_token, _provider) TP_ID1(_token, _provider)
490
491 static void __attribute__((constructor))
492 TP_ID(__lttng_events_init__, TRACEPOINT_PROVIDER)(void)
493 {
494 int ret;
495
496 ret = ltt_probe_register(&TP_ID(__probe_desc___, TRACEPOINT_PROVIDER));
497 assert(!ret);
498 }
499
500 static void __attribute__((destructor))
501 TP_ID(__lttng_events_exit__, TRACEPOINT_PROVIDER)(void)
502 {
503 ltt_probe_unregister(&TP_ID(__probe_desc___, TRACEPOINT_PROVIDER));
504 }
505
506 #undef TP_ID1
507 #undef TP_ID
This page took 0.042179 seconds and 5 git commands to generate.