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