Implement shm object table
[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 #undef TRACEPOINT_EVENT_CLASS_NOARGS
322 #define TRACEPOINT_EVENT_CLASS_NOARGS(_name, _fields) \
323 static inline size_t __event_get_size__##_name(size_t *__dynamic_len) \
324 { \
325 size_t __event_len = 0; \
326 unsigned int __dynamic_len_idx = 0; \
327 \
328 if (0) \
329 (void) __dynamic_len_idx; /* don't warn if unused */ \
330 _fields \
331 return __event_len; \
332 }
333
334 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
335
336 /*
337 * Stage 6 of the trace events.
338 *
339 * Create static inline function that calculates event payload alignment.
340 */
341
342 /* Reset all macros within TRACEPOINT_EVENT */
343 #include <ust/lttng-tracepoint-event-reset.h>
344
345 /* Named field types must be defined in lttng-types.h */
346
347 #undef ctf_integer_ext
348 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
349 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
350
351 #undef ctf_float
352 #define ctf_float(_type, _item, _src) \
353 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
354
355 #undef ctf_array_encoded
356 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
357 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
358
359 #undef ctf_sequence_encoded
360 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
361 _src_length, _encoding) \
362 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_length_type)); \
363 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
364
365 #undef ctf_string
366 #define ctf_string(_item, _src)
367
368 #undef TP_PROTO
369 #define TP_PROTO(args...) args
370
371 #undef TP_FIELDS
372 #define TP_FIELDS(args...) args
373
374 #undef TRACEPOINT_EVENT_CLASS
375 #define TRACEPOINT_EVENT_CLASS(_name, _proto, _args, _fields) \
376 static inline size_t __event_get_align__##_name(_proto) \
377 { \
378 size_t __event_align = 1; \
379 _fields \
380 return __event_align; \
381 }
382
383 #undef TRACEPOINT_EVENT_CLASS_NOARGS
384 #define TRACEPOINT_EVENT_CLASS_NOARGS(_name, _fields) \
385 static inline size_t __event_get_align__##_name(void) \
386 { \
387 size_t __event_align = 1; \
388 _fields \
389 return __event_align; \
390 }
391
392 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
393
394
395 /*
396 * Stage 7 of the trace events.
397 *
398 * Create the probe function : call even size calculation and write event data
399 * into the buffer.
400 *
401 * We use both the field and assignment macros to write the fields in the order
402 * defined in the field declaration. The field declarations control the
403 * execution order, jumping to the appropriate assignment block.
404 */
405
406 /* Reset all macros within TRACEPOINT_EVENT */
407 #include <ust/lttng-tracepoint-event-reset.h>
408
409 #undef ctf_integer_ext
410 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
411 { \
412 _type __tmp = (_src); \
413 lib_ring_buffer_align_ctx(&ctx, lttng_alignof(__tmp)); \
414 __chan->ops->event_write(&ctx, &__tmp, sizeof(__tmp)); \
415 }
416
417 #undef ctf_float
418 #define ctf_float(_type, _item, _src) \
419 { \
420 _type __tmp = (_src); \
421 lib_ring_buffer_align_ctx(&ctx, lttng_alignof(__tmp)); \
422 __chan->ops->event_write(&ctx, &__tmp, sizeof(__tmp)); \
423 }
424
425 #undef ctf_array_encoded
426 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
427 lib_ring_buffer_align_ctx(&ctx, lttng_alignof(_type)); \
428 __chan->ops->event_write(&ctx, _src, _length);
429
430 #undef ctf_sequence_encoded
431 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
432 _src_length, _encoding) \
433 { \
434 _length_type __tmpl = __dynamic_len[__dynamic_len_idx]; \
435 lib_ring_buffer_align_ctx(&ctx, lttng_alignof(_length_type)); \
436 __chan->ops->event_write(&ctx, &__tmpl, sizeof(_length_type)); \
437 } \
438 lib_ring_buffer_align_ctx(&ctx, lttng_alignof(_type)); \
439 __chan->ops->event_write(&ctx, _src, \
440 sizeof(_type) * __get_dynamic_len(dest));
441
442 #undef ctf_string
443 #define ctf_string(_item, _src) \
444 lib_ring_buffer_align_ctx(&ctx, lttng_alignof(*(_src))); \
445 __chan->ops->event_write(&ctx, _src, __get_dynamic_len(dest));
446
447 /* Beware: this get len actually consumes the len value */
448 #undef __get_dynamic_len
449 #define __get_dynamic_len(field) __dynamic_len[__dynamic_len_idx++]
450
451 #undef TP_PROTO
452 #define TP_PROTO(args...) args
453
454 #undef TP_ARGS
455 #define TP_ARGS(args...) args
456
457 #undef TP_FIELDS
458 #define TP_FIELDS(args...) args
459
460 #undef TRACEPOINT_EVENT_CLASS
461 #define TRACEPOINT_EVENT_CLASS(_name, _proto, _args, _fields) \
462 static void __event_probe__##_name(void *__data, _proto) \
463 { \
464 struct ltt_event *__event = __data; \
465 struct ltt_channel *__chan = __event->chan; \
466 struct lib_ring_buffer_ctx ctx; \
467 size_t __event_len, __event_align; \
468 size_t __dynamic_len_idx = 0; \
469 size_t __dynamic_len[_TP_ARRAY_SIZE(__event_fields___##_name)]; \
470 int __ret; \
471 \
472 if (0) \
473 (void) __dynamic_len_idx; /* don't warn if unused */ \
474 if (unlikely(!CMM_ACCESS_ONCE(__chan->session->active))) \
475 return; \
476 if (unlikely(!CMM_ACCESS_ONCE(__chan->enabled))) \
477 return; \
478 if (unlikely(!CMM_ACCESS_ONCE(__event->enabled))) \
479 return; \
480 __event_len = __event_get_size__##_name(__dynamic_len, _args); \
481 __event_align = __event_get_align__##_name(_args); \
482 lib_ring_buffer_ctx_init(&ctx, __chan->chan, __event, __event_len, \
483 __event_align, -1, __chan->handle); \
484 __ret = __chan->ops->event_reserve(&ctx, __event->id); \
485 if (__ret < 0) \
486 return; \
487 _fields \
488 __chan->ops->event_commit(&ctx); \
489 }
490
491 #undef TRACEPOINT_EVENT_CLASS_NOARGS
492 #define TRACEPOINT_EVENT_CLASS_NOARGS(_name, _fields) \
493 static void __event_probe__##_name(void *__data) \
494 { \
495 struct ltt_event *__event = __data; \
496 struct ltt_channel *__chan = __event->chan; \
497 struct lib_ring_buffer_ctx ctx; \
498 size_t __event_len, __event_align; \
499 size_t __dynamic_len_idx = 0; \
500 size_t __dynamic_len[_TP_ARRAY_SIZE(__event_fields___##_name)]; \
501 int __ret; \
502 \
503 if (0) \
504 (void) __dynamic_len_idx; /* don't warn if unused */ \
505 if (unlikely(!CMM_ACCESS_ONCE(__chan->session->active))) \
506 return; \
507 if (unlikely(!CMM_ACCESS_ONCE(__chan->enabled))) \
508 return; \
509 if (unlikely(!CMM_ACCESS_ONCE(__event->enabled))) \
510 return; \
511 __event_len = __event_get_size__##_name(__dynamic_len); \
512 __event_align = __event_get_align__##_name(); \
513 lib_ring_buffer_ctx_init(&ctx, __chan->chan, __event, __event_len, \
514 __event_align, -1, __chan->handle); \
515 __ret = __chan->ops->event_reserve(&ctx, __event->id); \
516 if (__ret < 0) \
517 return; \
518 _fields \
519 __chan->ops->event_commit(&ctx); \
520 }
521
522 #include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
523
524 #undef __get_dynamic_len
525
526 /*
527 * Stage 8 of the trace events.
528 *
529 * Register/unregister probes at module load/unload.
530 */
531
532 /* Reset all macros within TRACEPOINT_EVENT */
533 #include <ust/lttng-tracepoint-event-reset.h>
534
535 #define TP_ID1(_token, _system) _token##_system
536 #define TP_ID(_token, _system) TP_ID1(_token, _system)
537
538 static void __attribute__((constructor))
539 TP_ID(__lttng_events_init__, TRACEPOINT_SYSTEM)(void)
540 {
541 int ret;
542
543 ret = ltt_probe_register(&TP_ID(__probe_desc___, TRACEPOINT_SYSTEM));
544 assert(!ret);
545 }
546
547 static void __attribute__((destructor))
548 TP_ID(__lttng_events_exit__, TRACEPOINT_SYSTEM)(void)
549 {
550 ltt_probe_unregister(&TP_ID(__probe_desc___, TRACEPOINT_SYSTEM));
551 }
552
553 #undef TP_ID1
554 #undef TP_ID
This page took 0.040592 seconds and 4 git commands to generate.