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