Add lttng_ust_ prefix before objd_unref
[lttng-ust.git] / include / ust / lttng-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>
0a42beb6 22#include <ust/lttng-events.h>
1dbfff0c 23#include <ust/usterr-signal-safe.h>
8d8a24c8 24#include <ust/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
MD
111/* Reset all macros within TRACEPOINT_EVENT */
112#include <ust/lttng-tracepoint-event-reset.h>
0a42beb6
MD
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
20f1eee7
MD
123#undef ctf_float
124#define ctf_float(_type, _item, _src) \
125 { \
126 .name = #_item, \
127 .type = __type_float(_type), \
128 },
129
0a42beb6
MD
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
8d8a24c8
MD
175#undef TRACEPOINT_EVENT_CLASS_NOARGS
176#define TRACEPOINT_EVENT_CLASS_NOARGS(_name, _fields) \
0a42beb6
MD
177 static const struct lttng_event_field __event_fields___##_name[] = { \
178 _fields \
179 };
180
8d8a24c8
MD
181#undef TRACEPOINT_EVENT_CLASS
182#define TRACEPOINT_EVENT_CLASS(_name, _proto, _args, _fields) \
1dbfff0c 183 TRACEPOINT_EVENT_CLASS_NOARGS(_name, TP_PARAMS(_fields))
8d8a24c8
MD
184
185#include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
0a42beb6
MD
186
187/*
188 * Stage 2 of the trace events.
189 *
190 * Create probe callback prototypes.
191 */
192
8d8a24c8
MD
193/* Reset all macros within TRACEPOINT_EVENT */
194#include <ust/lttng-tracepoint-event-reset.h>
0a42beb6
MD
195
196#undef TP_PROTO
197#define TP_PROTO(args...) args
198
199#undef TRACEPOINT_EVENT_CLASS
8d8a24c8 200#define TRACEPOINT_EVENT_CLASS(_name, _proto, _args, _fields) \
0a42beb6
MD
201static void __event_probe__##_name(void *__data, _proto);
202
8d8a24c8
MD
203#undef TRACEPOINT_EVENT_CLASS_NOARGS
204#define TRACEPOINT_EVENT_CLASS_NOARGS(_name, _fields) \
205static void __event_probe__##_name(void *__data);
206
207#include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
0a42beb6
MD
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
8d8a24c8
MD
217/* Reset all macros within TRACEPOINT_EVENT */
218#include <ust/lttng-tracepoint-event-reset.h>
0a42beb6 219
8d8a24c8
MD
220#undef TRACEPOINT_EVENT_INSTANCE_NOARGS
221#define TRACEPOINT_EVENT_INSTANCE_NOARGS(_template, _name) \
0a42beb6
MD
222 { \
223 .fields = __event_fields___##_template, \
224 .name = #_name, \
225 .probe_callback = (void *) &__event_probe__##_template,\
8d8a24c8 226 .nr_fields = _TP_ARRAY_SIZE(__event_fields___##_template), \
0a42beb6
MD
227 },
228
8d8a24c8
MD
229#undef TRACEPOINT_EVENT_INSTANCE
230#define TRACEPOINT_EVENT_INSTANCE(_template, _name, _proto, _args) \
231 TRACEPOINT_EVENT_INSTANCE_NOARGS(_template, _name)
232
0a42beb6
MD
233#define TP_ID1(_token, _system) _token##_system
234#define TP_ID(_token, _system) TP_ID1(_token, _system)
235
8d8a24c8
MD
236static const struct lttng_event_desc TP_ID(__event_desc___, TRACEPOINT_SYSTEM)[] = {
237#include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
0a42beb6
MD
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. */
8d8a24c8
MD
254static 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)),
0a42beb6
MD
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
8d8a24c8
MD
268/* Reset all macros within TRACEPOINT_EVENT */
269#include <ust/lttng-tracepoint-event-reset.h>
0a42beb6
MD
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) \
1dbfff0c 275 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
0a42beb6
MD
276 __event_len += sizeof(_type);
277
20f1eee7
MD
278#undef ctf_float
279#define ctf_float(_type, _item, _src) \
1dbfff0c 280 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
20f1eee7
MD
281 __event_len += sizeof(_type);
282
0a42beb6 283#undef ctf_array_encoded
41aaf8a5 284#define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
1dbfff0c 285 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
0a42beb6
MD
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) \
1dbfff0c 291 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_length_type)); \
0a42beb6 292 __event_len += sizeof(_length_type); \
1dbfff0c 293 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
5dba5937 294 __dynamic_len[__dynamic_len_idx] = (_src_length); \
0a42beb6
MD
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) \
310static 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
8d8a24c8 321#include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
0a42beb6
MD
322
323/*
324 * Stage 6 of the trace events.
325 *
326 * Create static inline function that calculates event payload alignment.
327 */
328
8d8a24c8
MD
329/* Reset all macros within TRACEPOINT_EVENT */
330#include <ust/lttng-tracepoint-event-reset.h>
0a42beb6
MD
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) \
1dbfff0c 336 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
0a42beb6 337
20f1eee7
MD
338#undef ctf_float
339#define ctf_float(_type, _item, _src) \
1dbfff0c 340 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
20f1eee7 341
0a42beb6 342#undef ctf_array_encoded
41aaf8a5 343#define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
1dbfff0c 344 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
0a42beb6
MD
345
346#undef ctf_sequence_encoded
347#define ctf_sequence_encoded(_type, _item, _src, _length_type, \
348 _src_length, _encoding) \
1dbfff0c
MD
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));
0a42beb6
MD
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) \
363static inline size_t __event_get_align__##_name(_proto) \
364{ \
365 size_t __event_align = 1; \
366 _fields \
367 return __event_align; \
368}
369
8d8a24c8 370#include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
0a42beb6
MD
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
8d8a24c8
MD
384/* Reset all macros within TRACEPOINT_EVENT */
385#include <ust/lttng-tracepoint-event-reset.h>
0a42beb6
MD
386
387#undef ctf_integer_ext
388#define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
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));\
0a42beb6
MD
393 }
394
20f1eee7
MD
395#undef ctf_float
396#define ctf_float(_type, _item, _src) \
397 { \
398 _type __tmp = (_src); \
b8b77c00
MD
399 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
400 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
20f1eee7
MD
401 }
402
0a42beb6 403#undef ctf_array_encoded
41aaf8a5 404#define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
b8b77c00
MD
405 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
406 __chan->ops->event_write(&__ctx, _src, sizeof(_type) * (_length));
0a42beb6
MD
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]; \
b8b77c00
MD
413 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_length_type));\
414 __chan->ops->event_write(&__ctx, &__tmpl, sizeof(_length_type));\
0a42beb6 415 } \
b8b77c00
MD
416 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
417 __chan->ops->event_write(&__ctx, _src, \
5dba5937 418 sizeof(_type) * __get_dynamic_len(dest));
0a42beb6
MD
419
420#undef ctf_string
421#define ctf_string(_item, _src) \
b8b77c00
MD
422 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(*(_src))); \
423 __chan->ops->event_write(&__ctx, _src, __get_dynamic_len(dest));
0a42beb6
MD
424
425/* Beware: this get len actually consumes the len value */
5dba5937
MD
426#undef __get_dynamic_len
427#define __get_dynamic_len(field) __dynamic_len[__dynamic_len_idx++]
0a42beb6
MD
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) \
440static void __event_probe__##_name(void *__data, _proto) \
441{ \
442 struct ltt_event *__event = __data; \
443 struct ltt_channel *__chan = __event->chan; \
4cfec15c 444 struct lttng_ust_lib_ring_buffer_ctx __ctx; \
0a42beb6
MD
445 size_t __event_len, __event_align; \
446 size_t __dynamic_len_idx = 0; \
8d8a24c8 447 size_t __dynamic_len[_TP_ARRAY_SIZE(__event_fields___##_name)]; \
0a42beb6
MD
448 int __ret; \
449 \
450 if (0) \
451 (void) __dynamic_len_idx; /* don't warn if unused */ \
b5a3dfa5 452 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->session->active))) \
0a42beb6 453 return; \
b5a3dfa5 454 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->enabled))) \
0a42beb6 455 return; \
b5a3dfa5 456 if (caa_unlikely(!CMM_ACCESS_ONCE(__event->enabled))) \
0a42beb6
MD
457 return; \
458 __event_len = __event_get_size__##_name(__dynamic_len, _args); \
459 __event_align = __event_get_align__##_name(_args); \
b8b77c00 460 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
1d498196 461 __event_align, -1, __chan->handle); \
b8b77c00 462 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
0a42beb6
MD
463 if (__ret < 0) \
464 return; \
465 _fields \
b8b77c00 466 __chan->ops->event_commit(&__ctx); \
0a42beb6
MD
467}
468
8d8a24c8
MD
469#undef TRACEPOINT_EVENT_CLASS_NOARGS
470#define TRACEPOINT_EVENT_CLASS_NOARGS(_name, _fields) \
471static void __event_probe__##_name(void *__data) \
472{ \
473 struct ltt_event *__event = __data; \
474 struct ltt_channel *__chan = __event->chan; \
4cfec15c 475 struct lttng_ust_lib_ring_buffer_ctx __ctx; \
8d8a24c8 476 size_t __event_len, __event_align; \
8d8a24c8
MD
477 int __ret; \
478 \
b5a3dfa5 479 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->session->active))) \
8d8a24c8 480 return; \
b5a3dfa5 481 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->enabled))) \
8d8a24c8 482 return; \
b5a3dfa5 483 if (caa_unlikely(!CMM_ACCESS_ONCE(__event->enabled))) \
8d8a24c8 484 return; \
4f6ffb56
MD
485 __event_len = 0; \
486 __event_align = 1; \
b8b77c00 487 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
1d498196 488 __event_align, -1, __chan->handle); \
b8b77c00 489 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
8d8a24c8
MD
490 if (__ret < 0) \
491 return; \
492 _fields \
b8b77c00 493 __chan->ops->event_commit(&__ctx); \
8d8a24c8
MD
494}
495
496#include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
0a42beb6 497
5dba5937
MD
498#undef __get_dynamic_len
499
0a42beb6
MD
500/*
501 * Stage 8 of the trace events.
502 *
503 * Register/unregister probes at module load/unload.
504 */
505
8d8a24c8
MD
506/* Reset all macros within TRACEPOINT_EVENT */
507#include <ust/lttng-tracepoint-event-reset.h>
0a42beb6
MD
508
509#define TP_ID1(_token, _system) _token##_system
510#define TP_ID(_token, _system) TP_ID1(_token, _system)
511
512static void __attribute__((constructor))
8d8a24c8 513TP_ID(__lttng_events_init__, TRACEPOINT_SYSTEM)(void)
0a42beb6
MD
514{
515 int ret;
516
8d8a24c8 517 ret = ltt_probe_register(&TP_ID(__probe_desc___, TRACEPOINT_SYSTEM));
0a42beb6
MD
518 assert(!ret);
519}
520
521static void __attribute__((destructor))
8d8a24c8 522TP_ID(__lttng_events_exit__, TRACEPOINT_SYSTEM)(void)
0a42beb6 523{
8d8a24c8 524 ltt_probe_unregister(&TP_ID(__probe_desc___, TRACEPOINT_SYSTEM));
0a42beb6
MD
525}
526
527#undef TP_ID1
528#undef TP_ID
This page took 0.045201 seconds and 4 git commands to generate.