library mass rename: add lttng- prefix
[lttng-ust.git] / include / lttng / ust-tracepoint-event.h
CommitLineData
0a42beb6
MD
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>
8d8a24c8 21#include <urcu/compiler.h>
4318ae1b
MD
22#include <lttng/ust-events.h>
23#include <lttng/usterr-signal-safe.h>
24#include <lttng/ringbuffer-config.h>
0a42beb6
MD
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
513fc97e
MD
46/* ctf_float is redefined at each step */
47
0a42beb6
MD
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
8d8a24c8
MD
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
1dbfff0c
MD
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
0a42beb6
MD
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
8d8a24c8 111/* Reset all macros within TRACEPOINT_EVENT */
4318ae1b 112#include <lttng/ust-tracepoint-event-reset.h>
0a42beb6
MD
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
20f1eee7
MD
121#undef ctf_float
122#define ctf_float(_type, _item, _src) \
123 { \
124 .name = #_item, \
125 .type = __type_float(_type), \
126 },
127
0a42beb6
MD
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
8d8a24c8
MD
173#undef TRACEPOINT_EVENT_CLASS_NOARGS
174#define TRACEPOINT_EVENT_CLASS_NOARGS(_name, _fields) \
0a42beb6
MD
175 static const struct lttng_event_field __event_fields___##_name[] = { \
176 _fields \
177 };
178
8d8a24c8
MD
179#undef TRACEPOINT_EVENT_CLASS
180#define TRACEPOINT_EVENT_CLASS(_name, _proto, _args, _fields) \
1dbfff0c 181 TRACEPOINT_EVENT_CLASS_NOARGS(_name, TP_PARAMS(_fields))
8d8a24c8
MD
182
183#include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
0a42beb6
MD
184
185/*
186 * Stage 2 of the trace events.
187 *
188 * Create probe callback prototypes.
189 */
190
8d8a24c8 191/* Reset all macros within TRACEPOINT_EVENT */
4318ae1b 192#include <lttng/ust-tracepoint-event-reset.h>
0a42beb6
MD
193
194#undef TP_PROTO
195#define TP_PROTO(args...) args
196
197#undef TRACEPOINT_EVENT_CLASS
8d8a24c8 198#define TRACEPOINT_EVENT_CLASS(_name, _proto, _args, _fields) \
0a42beb6
MD
199static void __event_probe__##_name(void *__data, _proto);
200
8d8a24c8
MD
201#undef TRACEPOINT_EVENT_CLASS_NOARGS
202#define TRACEPOINT_EVENT_CLASS_NOARGS(_name, _fields) \
203static void __event_probe__##_name(void *__data);
204
205#include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
0a42beb6
MD
206
207/*
208 * Stage 3 of the trace events.
209 *
210 * Create an array of events.
211 */
212
8d8a24c8 213/* Reset all macros within TRACEPOINT_EVENT */
4318ae1b 214#include <lttng/ust-tracepoint-event-reset.h>
0a42beb6 215
8d8a24c8
MD
216#undef TRACEPOINT_EVENT_INSTANCE_NOARGS
217#define TRACEPOINT_EVENT_INSTANCE_NOARGS(_template, _name) \
0a42beb6
MD
218 { \
219 .fields = __event_fields___##_template, \
220 .name = #_name, \
221 .probe_callback = (void *) &__event_probe__##_template,\
8d8a24c8 222 .nr_fields = _TP_ARRAY_SIZE(__event_fields___##_template), \
0a42beb6
MD
223 },
224
8d8a24c8
MD
225#undef TRACEPOINT_EVENT_INSTANCE
226#define TRACEPOINT_EVENT_INSTANCE(_template, _name, _proto, _args) \
227 TRACEPOINT_EVENT_INSTANCE_NOARGS(_template, _name)
228
0a42beb6
MD
229#define TP_ID1(_token, _system) _token##_system
230#define TP_ID(_token, _system) TP_ID1(_token, _system)
231
8d8a24c8
MD
232static const struct lttng_event_desc TP_ID(__event_desc___, TRACEPOINT_SYSTEM)[] = {
233#include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
0a42beb6
MD
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, _system) _token##_system
247#define TP_ID(_token, _system) TP_ID1(_token, _system)
248
249/* non-const because list head will be modified when registered. */
8d8a24c8
MD
250static struct lttng_probe_desc TP_ID(__probe_desc___, TRACEPOINT_SYSTEM) = {
251 .event_desc = TP_ID(__event_desc___, TRACEPOINT_SYSTEM),
252 .nr_events = _TP_ARRAY_SIZE(TP_ID(__event_desc___, TRACEPOINT_SYSTEM)),
0a42beb6
MD
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
8d8a24c8 264/* Reset all macros within TRACEPOINT_EVENT */
4318ae1b 265#include <lttng/ust-tracepoint-event-reset.h>
0a42beb6
MD
266
267#undef ctf_integer_ext
268#define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
1dbfff0c 269 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
0a42beb6
MD
270 __event_len += sizeof(_type);
271
20f1eee7
MD
272#undef ctf_float
273#define ctf_float(_type, _item, _src) \
1dbfff0c 274 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
20f1eee7
MD
275 __event_len += sizeof(_type);
276
0a42beb6 277#undef ctf_array_encoded
41aaf8a5 278#define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
1dbfff0c 279 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
0a42beb6
MD
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) \
1dbfff0c 285 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_length_type)); \
0a42beb6 286 __event_len += sizeof(_length_type); \
1dbfff0c 287 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
5dba5937 288 __dynamic_len[__dynamic_len_idx] = (_src_length); \
0a42beb6
MD
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(_name, _proto, _args, _fields) \
304static inline size_t __event_get_size__##_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
8d8a24c8 315#include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
0a42beb6
MD
316
317/*
318 * Stage 6 of the trace events.
319 *
320 * Create static inline function that calculates event payload alignment.
321 */
322
8d8a24c8 323/* Reset all macros within TRACEPOINT_EVENT */
4318ae1b 324#include <lttng/ust-tracepoint-event-reset.h>
0a42beb6
MD
325
326#undef ctf_integer_ext
327#define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
1dbfff0c 328 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
0a42beb6 329
20f1eee7
MD
330#undef ctf_float
331#define ctf_float(_type, _item, _src) \
1dbfff0c 332 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
20f1eee7 333
0a42beb6 334#undef ctf_array_encoded
41aaf8a5 335#define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
1dbfff0c 336 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
0a42beb6
MD
337
338#undef ctf_sequence_encoded
339#define ctf_sequence_encoded(_type, _item, _src, _length_type, \
340 _src_length, _encoding) \
1dbfff0c
MD
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));
0a42beb6
MD
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(_name, _proto, _args, _fields) \
355static inline size_t __event_get_align__##_name(_proto) \
356{ \
357 size_t __event_align = 1; \
358 _fields \
359 return __event_align; \
360}
361
8d8a24c8 362#include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
0a42beb6
MD
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
8d8a24c8 376/* Reset all macros within TRACEPOINT_EVENT */
4318ae1b 377#include <lttng/ust-tracepoint-event-reset.h>
0a42beb6
MD
378
379#undef ctf_integer_ext
380#define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
381 { \
382 _type __tmp = (_src); \
b8b77c00
MD
383 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
384 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
0a42beb6
MD
385 }
386
20f1eee7
MD
387#undef ctf_float
388#define ctf_float(_type, _item, _src) \
389 { \
390 _type __tmp = (_src); \
b8b77c00
MD
391 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
392 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
20f1eee7
MD
393 }
394
0a42beb6 395#undef ctf_array_encoded
41aaf8a5 396#define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
b8b77c00
MD
397 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
398 __chan->ops->event_write(&__ctx, _src, sizeof(_type) * (_length));
0a42beb6
MD
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]; \
b8b77c00
MD
405 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_length_type));\
406 __chan->ops->event_write(&__ctx, &__tmpl, sizeof(_length_type));\
0a42beb6 407 } \
b8b77c00
MD
408 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
409 __chan->ops->event_write(&__ctx, _src, \
5dba5937 410 sizeof(_type) * __get_dynamic_len(dest));
0a42beb6
MD
411
412#undef ctf_string
413#define ctf_string(_item, _src) \
b8b77c00
MD
414 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(*(_src))); \
415 __chan->ops->event_write(&__ctx, _src, __get_dynamic_len(dest));
0a42beb6
MD
416
417/* Beware: this get len actually consumes the len value */
5dba5937
MD
418#undef __get_dynamic_len
419#define __get_dynamic_len(field) __dynamic_len[__dynamic_len_idx++]
0a42beb6
MD
420
421#undef TP_PROTO
422#define TP_PROTO(args...) args
423
424#undef TP_ARGS
425#define TP_ARGS(args...) args
426
427#undef TP_FIELDS
428#define TP_FIELDS(args...) args
429
430#undef TRACEPOINT_EVENT_CLASS
431#define TRACEPOINT_EVENT_CLASS(_name, _proto, _args, _fields) \
432static void __event_probe__##_name(void *__data, _proto) \
433{ \
434 struct ltt_event *__event = __data; \
435 struct ltt_channel *__chan = __event->chan; \
4cfec15c 436 struct lttng_ust_lib_ring_buffer_ctx __ctx; \
0a42beb6
MD
437 size_t __event_len, __event_align; \
438 size_t __dynamic_len_idx = 0; \
8d8a24c8 439 size_t __dynamic_len[_TP_ARRAY_SIZE(__event_fields___##_name)]; \
0a42beb6
MD
440 int __ret; \
441 \
442 if (0) \
443 (void) __dynamic_len_idx; /* don't warn if unused */ \
b5a3dfa5 444 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->session->active))) \
0a42beb6 445 return; \
b5a3dfa5 446 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->enabled))) \
0a42beb6 447 return; \
b5a3dfa5 448 if (caa_unlikely(!CMM_ACCESS_ONCE(__event->enabled))) \
0a42beb6
MD
449 return; \
450 __event_len = __event_get_size__##_name(__dynamic_len, _args); \
451 __event_align = __event_get_align__##_name(_args); \
b8b77c00 452 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
1d498196 453 __event_align, -1, __chan->handle); \
b8b77c00 454 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
0a42beb6
MD
455 if (__ret < 0) \
456 return; \
457 _fields \
b8b77c00 458 __chan->ops->event_commit(&__ctx); \
0a42beb6
MD
459}
460
8d8a24c8
MD
461#undef TRACEPOINT_EVENT_CLASS_NOARGS
462#define TRACEPOINT_EVENT_CLASS_NOARGS(_name, _fields) \
463static void __event_probe__##_name(void *__data) \
464{ \
465 struct ltt_event *__event = __data; \
466 struct ltt_channel *__chan = __event->chan; \
4cfec15c 467 struct lttng_ust_lib_ring_buffer_ctx __ctx; \
8d8a24c8 468 size_t __event_len, __event_align; \
8d8a24c8
MD
469 int __ret; \
470 \
b5a3dfa5 471 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->session->active))) \
8d8a24c8 472 return; \
b5a3dfa5 473 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->enabled))) \
8d8a24c8 474 return; \
b5a3dfa5 475 if (caa_unlikely(!CMM_ACCESS_ONCE(__event->enabled))) \
8d8a24c8 476 return; \
4f6ffb56
MD
477 __event_len = 0; \
478 __event_align = 1; \
b8b77c00 479 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
1d498196 480 __event_align, -1, __chan->handle); \
b8b77c00 481 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
8d8a24c8
MD
482 if (__ret < 0) \
483 return; \
484 _fields \
b8b77c00 485 __chan->ops->event_commit(&__ctx); \
8d8a24c8
MD
486}
487
488#include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
0a42beb6 489
5dba5937
MD
490#undef __get_dynamic_len
491
0a42beb6
MD
492/*
493 * Stage 8 of the trace events.
494 *
495 * Register/unregister probes at module load/unload.
496 */
497
8d8a24c8 498/* Reset all macros within TRACEPOINT_EVENT */
4318ae1b 499#include <lttng/ust-tracepoint-event-reset.h>
0a42beb6
MD
500
501#define TP_ID1(_token, _system) _token##_system
502#define TP_ID(_token, _system) TP_ID1(_token, _system)
503
504static void __attribute__((constructor))
8d8a24c8 505TP_ID(__lttng_events_init__, TRACEPOINT_SYSTEM)(void)
0a42beb6
MD
506{
507 int ret;
508
8d8a24c8 509 ret = ltt_probe_register(&TP_ID(__probe_desc___, TRACEPOINT_SYSTEM));
0a42beb6
MD
510 assert(!ret);
511}
512
513static void __attribute__((destructor))
8d8a24c8 514TP_ID(__lttng_events_exit__, TRACEPOINT_SYSTEM)(void)
0a42beb6 515{
8d8a24c8 516 ltt_probe_unregister(&TP_ID(__probe_desc___, TRACEPOINT_SYSTEM));
0a42beb6
MD
517}
518
519#undef TP_ID1
520#undef TP_ID
This page took 0.047138 seconds and 4 git commands to generate.