Fix static provider linking: introduce TRACEPOINT_PROBE_DYNAMIC_LINKAGE
[lttng-ust.git] / include / lttng / ust-tracepoint-event.h
CommitLineData
1c324e59
MD
1/*
2 * Copyright (c) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 *
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
6 *
7 * Permission is hereby granted to use or copy this program
8 * for any purpose, provided the above notices are retained on all copies.
9 * Permission to modify the code and to distribute modified code is granted,
10 * provided the above notices are retained, and a notice that the code was
11 * modified is included with the above copyright notice.
12 */
13
14#include <stdio.h>
15#include <urcu/compiler.h>
16#include <lttng/ust-events.h>
1c324e59 17#include <lttng/ringbuffer-config.h>
44c72f10 18#include <string.h>
1c324e59
MD
19
20/*
21 * Macro declarations used for all stages.
22 */
23
24#undef ctf_integer
25#define ctf_integer(_type, _item, _src) \
26 ctf_integer_ext(_type, _item, _src, BYTE_ORDER, 10)
27
28#undef ctf_integer_hex
29#define ctf_integer_hex(_type, _item, _src) \
30 ctf_integer_ext(_type, _item, _src, BYTE_ORDER, 16)
31
32#undef ctf_integer_network
33#define ctf_integer_network(_type, _item, _src) \
34 ctf_integer_ext(_type, _item, _src, BIG_ENDIAN, 10)
35
36#undef ctf_integer_network_hex
37#define ctf_integer_network_hex(_type, _item, _src) \
38 ctf_integer_ext(_type, _item, _src, BIG_ENDIAN, 16)
39
40/* ctf_float is redefined at each step */
41
42#undef ctf_array
43#define ctf_array(_type, _item, _src, _length) \
44 ctf_array_encoded(_type, _item, _src, _length, none)
45
46#undef ctf_array_text
47#define ctf_array_text(_type, _item, _src, _length) \
48 ctf_array_encoded(_type, _item, _src, _length, UTF8)
49
50#undef ctf_sequence
51#define ctf_sequence(_type, _item, _src, _length_type, _src_length) \
52 ctf_sequence_encoded(_type, _item, _src, \
53 _length_type, _src_length, none)
54
55#undef ctf_sequence_text
56#define ctf_sequence_text(_type, _item, _src, _length_type, _src_length) \
57 ctf_sequence_encoded(_type, _item, _src, \
58 _length_type, _src_length, UTF8)
59
60/* ctf_string is redefined at each step */
61
62/*
63 * TRACEPOINT_EVENT_CLASS declares a class of tracepoints receiving the
64 * same arguments and having the same field layout.
65 *
66 * TRACEPOINT_EVENT_INSTANCE declares an instance of a tracepoint, with
67 * its own provider and name. It refers to a class (template).
68 *
69 * TRACEPOINT_EVENT declared both a class and an instance and does a
70 * direct mapping from the instance to the class.
71 */
72
73#undef TRACEPOINT_EVENT
74#define TRACEPOINT_EVENT(_provider, _name, _args, _fields) \
75 TRACEPOINT_EVENT_CLASS(_provider, _name, \
76 _TP_PARAMS(_args), \
77 _TP_PARAMS(_fields)) \
78 TRACEPOINT_EVENT_INSTANCE(_provider, _name, _name, \
79 _TP_PARAMS(args))
80
81/* Helpers */
82#define _TP_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
83
84#define _tp_max_t(type, x, y) \
85 ({ \
86 type __max1 = (x); \
87 type __max2 = (y); \
88 __max1 > __max2 ? __max1: __max2; \
89 })
90
91/*
92 * Stage 0 of tracepoint event generation.
93 *
94 * Check that each TRACEPOINT_EVENT provider argument match the
95 * TRACEPOINT_PROVIDER by creating dummy callbacks.
96 */
97
98/* Reset all macros within TRACEPOINT_EVENT */
99#include <lttng/ust-tracepoint-event-reset.h>
100
101static inline
102void _TP_COMBINE_TOKENS(__tracepoint_provider_mismatch_, TRACEPOINT_PROVIDER)(void)
103{
104}
105
106#undef TRACEPOINT_EVENT_CLASS
107#define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
108 __tracepoint_provider_mismatch_##_provider();
109
110#undef TRACEPOINT_EVENT_INSTANCE
111#define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
112 __tracepoint_provider_mismatch_##_provider();
113
114static __attribute__((unused))
115void _TP_COMBINE_TOKENS(__tracepoint_provider_check_, TRACEPOINT_PROVIDER)(void)
116{
117#include TRACEPOINT_INCLUDE
118}
119
f56cd1d5
MD
120/*
121 * Stage 0.1 of tracepoint event generation.
122 *
123 * Check that each TRACEPOINT_EVENT provider:name does not exceed the
124 * tracepoint name length limit.
125 */
126
127/* Reset all macros within TRACEPOINT_EVENT */
128#include <lttng/ust-tracepoint-event-reset.h>
129
130#undef TRACEPOINT_EVENT_INSTANCE
131#define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
132static const char \
133 __tp_name_len_check##_provider##___##_name[LTTNG_UST_SYM_NAME_LEN] \
134 __attribute__((unused)) = \
135 #_provider ":" #_name;
136
137#include TRACEPOINT_INCLUDE
138
1c324e59
MD
139/*
140 * Stage 1 of tracepoint event generation.
141 *
142 * Create event field type metadata section.
143 * Each event produce an array of fields.
144 */
145
146/* Reset all macros within TRACEPOINT_EVENT */
147#include <lttng/ust-tracepoint-event-reset.h>
148
149#undef ctf_integer_ext
150#define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
151 { \
152 .name = #_item, \
153 .type = __type_integer(_type, _byte_order, _base, none),\
154 },
155
156#undef ctf_float
157#define ctf_float(_type, _item, _src) \
158 { \
159 .name = #_item, \
160 .type = __type_float(_type), \
161 },
162
163#undef ctf_array_encoded
164#define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
165 { \
166 .name = #_item, \
167 .type = \
168 { \
169 .atype = atype_array, \
170 .u.array = \
171 { \
172 .length = _length, \
173 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
174 }, \
175 }, \
176 },
177
178#undef ctf_sequence_encoded
179#define ctf_sequence_encoded(_type, _item, _src, \
180 _length_type, _src_length, _encoding) \
181 { \
182 .name = #_item, \
183 .type = \
184 { \
185 .atype = atype_sequence, \
186 .u.sequence = \
187 { \
188 .length_type = __type_integer(_length_type, BYTE_ORDER, 10, none), \
189 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
190 }, \
191 }, \
192 },
193
194#undef ctf_string
195#define ctf_string(_item, _src) \
196 { \
197 .name = #_item, \
198 .type = \
199 { \
200 .atype = atype_string, \
201 .u.basic.string.encoding = lttng_encode_UTF8, \
202 }, \
203 },
204
205#undef TP_FIELDS
206#define TP_FIELDS(args...) args /* Only one used in this phase */
207
208#undef TRACEPOINT_EVENT_CLASS
209#define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
210 static const struct lttng_event_field __event_fields___##_provider##___##_name[] = { \
211 _fields \
212 };
213
214#include TRACEPOINT_INCLUDE
215
216/*
217 * Stage 2 of tracepoint event generation.
218 *
219 * Create probe callback prototypes.
220 */
221
222/* Reset all macros within TRACEPOINT_EVENT */
223#include <lttng/ust-tracepoint-event-reset.h>
224
225#undef TP_ARGS
226#define TP_ARGS(args...) args
227
228#undef TRACEPOINT_EVENT_CLASS
229#define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
230static void __event_probe__##_provider##___##_name(_TP_ARGS_DATA_PROTO(_args));
231
232#include TRACEPOINT_INCLUDE
233
234/*
235 * Stage 3 of tracepoint event generation.
236 *
1c324e59
MD
237 * Create static inline function that calculates event size.
238 */
239
240/* Reset all macros within TRACEPOINT_EVENT */
241#include <lttng/ust-tracepoint-event-reset.h>
242
243#undef ctf_integer_ext
244#define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
245 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
246 __event_len += sizeof(_type);
247
248#undef ctf_float
249#define ctf_float(_type, _item, _src) \
250 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
251 __event_len += sizeof(_type);
252
253#undef ctf_array_encoded
254#define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
255 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
256 __event_len += sizeof(_type) * (_length);
257
258#undef ctf_sequence_encoded
259#define ctf_sequence_encoded(_type, _item, _src, _length_type, \
260 _src_length, _encoding) \
261 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_length_type)); \
262 __event_len += sizeof(_length_type); \
263 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
264 __dynamic_len[__dynamic_len_idx] = (_src_length); \
265 __event_len += sizeof(_type) * __dynamic_len[__dynamic_len_idx]; \
266 __dynamic_len_idx++;
267
268#undef ctf_string
269#define ctf_string(_item, _src) \
270 __event_len += __dynamic_len[__dynamic_len_idx++] = strlen(_src) + 1;
271
272#undef TP_ARGS
273#define TP_ARGS(args...) args
274
275#undef TP_FIELDS
276#define TP_FIELDS(args...) args
277
278#undef TRACEPOINT_EVENT_CLASS
279#define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
280static inline size_t __event_get_size__##_provider##___##_name(size_t *__dynamic_len, _TP_ARGS_DATA_PROTO(_args)) \
281{ \
282 size_t __event_len = 0; \
283 unsigned int __dynamic_len_idx = 0; \
284 \
285 if (0) \
286 (void) __dynamic_len_idx; /* don't warn if unused */ \
287 _fields \
288 return __event_len; \
289}
290
291#include TRACEPOINT_INCLUDE
292
293/*
df854e41 294 * Stage 4 of tracepoint event generation.
1c324e59
MD
295 *
296 * Create static inline function that calculates event payload alignment.
297 */
298
299/* Reset all macros within TRACEPOINT_EVENT */
300#include <lttng/ust-tracepoint-event-reset.h>
301
302#undef ctf_integer_ext
303#define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
304 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
305
306#undef ctf_float
307#define ctf_float(_type, _item, _src) \
308 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
309
310#undef ctf_array_encoded
311#define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
312 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
313
314#undef ctf_sequence_encoded
315#define ctf_sequence_encoded(_type, _item, _src, _length_type, \
316 _src_length, _encoding) \
317 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_length_type)); \
318 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
319
320#undef ctf_string
321#define ctf_string(_item, _src)
322
323#undef TP_ARGS
324#define TP_ARGS(args...) args
325
326#undef TP_FIELDS
327#define TP_FIELDS(args...) args
328
329#undef TRACEPOINT_EVENT_CLASS
330#define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
331static inline \
332size_t __event_get_align__##_provider##___##_name(_TP_ARGS_PROTO(_args)) \
333{ \
334 size_t __event_align = 1; \
335 _fields \
336 return __event_align; \
337}
338
339#include TRACEPOINT_INCLUDE
340
341
342/*
df854e41 343 * Stage 5 of tracepoint event generation.
1c324e59
MD
344 *
345 * Create the probe function. This function calls event size calculation
346 * and writes event data into the buffer.
347 */
348
349/* Reset all macros within TRACEPOINT_EVENT */
350#include <lttng/ust-tracepoint-event-reset.h>
351
352#undef ctf_integer_ext
353#define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
354 { \
355 _type __tmp = (_src); \
356 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
357 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
358 }
359
360#undef ctf_float
361#define ctf_float(_type, _item, _src) \
362 { \
363 _type __tmp = (_src); \
364 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
365 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
366 }
367
368#undef ctf_array_encoded
369#define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
370 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
371 __chan->ops->event_write(&__ctx, _src, sizeof(_type) * (_length));
372
373#undef ctf_sequence_encoded
374#define ctf_sequence_encoded(_type, _item, _src, _length_type, \
375 _src_length, _encoding) \
376 { \
377 _length_type __tmpl = __dynamic_len[__dynamic_len_idx]; \
378 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_length_type));\
379 __chan->ops->event_write(&__ctx, &__tmpl, sizeof(_length_type));\
380 } \
381 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
382 __chan->ops->event_write(&__ctx, _src, \
383 sizeof(_type) * __get_dynamic_len(dest));
384
385#undef ctf_string
386#define ctf_string(_item, _src) \
387 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(*(_src))); \
388 __chan->ops->event_write(&__ctx, _src, __get_dynamic_len(dest));
389
390/* Beware: this get len actually consumes the len value */
391#undef __get_dynamic_len
392#define __get_dynamic_len(field) __dynamic_len[__dynamic_len_idx++]
393
394#undef TP_ARGS
395#define TP_ARGS(args...) args
396
397#undef TP_FIELDS
398#define TP_FIELDS(args...) args
399
400#undef TRACEPOINT_EVENT_CLASS
401#define TRACEPOINT_EVENT_CLASS(_provider, _name, _args, _fields) \
402static void __event_probe__##_provider##___##_name(_TP_ARGS_DATA_PROTO(_args))\
403{ \
f280cb51 404 struct ltt_event *__event = __tp_data; \
1c324e59 405 struct ltt_channel *__chan = __event->chan; \
f280cb51 406 struct lttng_ust_lib_ring_buffer_ctx __ctx; \
1c324e59
MD
407 size_t __event_len, __event_align; \
408 size_t __dynamic_len_idx = 0; \
409 size_t __dynamic_len[_TP_ARRAY_SIZE(__event_fields___##_provider##___##_name)]; \
410 int __ret; \
411 \
412 if (0) \
413 (void) __dynamic_len_idx; /* don't warn if unused */ \
414 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->session->active))) \
415 return; \
416 if (caa_unlikely(!CMM_ACCESS_ONCE(__chan->enabled))) \
417 return; \
418 if (caa_unlikely(!CMM_ACCESS_ONCE(__event->enabled))) \
419 return; \
420 __event_len = __event_get_size__##_provider##___##_name(__dynamic_len,\
421 _TP_ARGS_DATA_VAR(_args)); \
422 __event_align = __event_get_align__##_provider##___##_name(_TP_ARGS_VAR(_args)); \
423 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
424 __event_align, -1, __chan->handle); \
425 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
426 if (__ret < 0) \
427 return; \
428 _fields \
429 __chan->ops->event_commit(&__ctx); \
430}
431
432#include TRACEPOINT_INCLUDE
433
434#undef __get_dynamic_len
435
05ceaafd 436/*
882a56d7 437 * Stage 6 of tracepoint event generation.
1c324e59 438 *
df854e41
MD
439 * Tracepoint loglevel mapping definition generation. We generate a
440 * symbol for each mapping for a provider/event to ensure at most a 1 to
441 * 1 mapping between events and loglevels. If the symbol is repeated,
442 * the compiler will complain.
05ceaafd
MD
443 */
444
445/* Reset all macros within TRACEPOINT_EVENT */
446#include <lttng/ust-tracepoint-event-reset.h>
447
448#undef TRACEPOINT_LOGLEVEL
457a6b58
MD
449#define TRACEPOINT_LOGLEVEL(__provider, __name, __loglevel) \
450static const int _loglevel_value___##__provider##___##__name = __loglevel; \
451static const int *_loglevel___##__provider##___##__name = \
452 &_loglevel_value___##__provider##___##__name;
df854e41
MD
453
454#include TRACEPOINT_INCLUDE
455
456/*
882a56d7 457 * Stage 7.1 of tracepoint event generation.
df854e41
MD
458 *
459 * Create events description structures. We use a weakref because
460 * loglevels are optional. If not declared, the event will point to the
461 * a loglevel that contains NULL.
462 */
463
464/* Reset all macros within TRACEPOINT_EVENT */
465#include <lttng/ust-tracepoint-event-reset.h>
466
467#undef TRACEPOINT_EVENT_INSTANCE
468#define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
882a56d7
MD
469static const int * \
470 __ref_loglevel___##_provider##___##_name \
471 __attribute__((weakref ("_loglevel___" #_provider "___" #_name))); \
df854e41
MD
472const struct lttng_event_desc __event_desc___##_provider##_##_name = { \
473 .fields = __event_fields___##_provider##___##_template, \
474 .name = #_provider ":" #_name, \
475 .probe_callback = (void *) &__event_probe__##_provider##___##_template,\
476 .nr_fields = _TP_ARRAY_SIZE(__event_fields___##_provider##___##_template), \
882a56d7 477 .loglevel = &__ref_loglevel___##_provider##___##_name, \
df854e41
MD
478};
479
480#include TRACEPOINT_INCLUDE
05ceaafd 481
df854e41 482/*
882a56d7 483 * Stage 7.2 of tracepoint event generation.
df854e41
MD
484 *
485 * Create array of events.
486 */
487
488/* Reset all macros within TRACEPOINT_EVENT */
489#include <lttng/ust-tracepoint-event-reset.h>
490
491#undef TRACEPOINT_EVENT_INSTANCE
492#define TRACEPOINT_EVENT_INSTANCE(_provider, _template, _name, _args) \
493 &__event_desc___##_provider##_##_name,
494
495static const struct lttng_event_desc *_TP_COMBINE_TOKENS(__event_desc___, TRACEPOINT_PROVIDER)[] = {
05ceaafd
MD
496#include TRACEPOINT_INCLUDE
497};
498
df854e41 499
05ceaafd 500/*
882a56d7 501 * Stage 8 of tracepoint event generation.
05ceaafd
MD
502 *
503 * Create a toplevel descriptor for the whole probe.
504 */
505
506/* non-const because list head will be modified when registered. */
507static struct lttng_probe_desc _TP_COMBINE_TOKENS(__probe_desc___, TRACEPOINT_PROVIDER) = {
df854e41 508 .provider = __tp_stringify(TRACEPOINT_PROVIDER),
05ceaafd
MD
509 .event_desc = _TP_COMBINE_TOKENS(__event_desc___, TRACEPOINT_PROVIDER),
510 .nr_events = _TP_ARRAY_SIZE(_TP_COMBINE_TOKENS(__event_desc___, TRACEPOINT_PROVIDER)),
05ceaafd
MD
511};
512
513/*
882a56d7 514 * Stage 9 of tracepoint event generation.
05ceaafd 515 *
1c324e59 516 * Register/unregister probes at module load/unload.
628e1d81
MD
517 *
518 * Generate the constructor as an externally visible symbol for use when
519 * linking the probe statically.
1c324e59
MD
520 */
521
522/* Reset all macros within TRACEPOINT_EVENT */
523#include <lttng/ust-tracepoint-event-reset.h>
1c324e59
MD
524static void __attribute__((constructor))
525_TP_COMBINE_TOKENS(__lttng_events_init__, TRACEPOINT_PROVIDER)(void)
526{
527 int ret;
528
529 ret = ltt_probe_register(&_TP_COMBINE_TOKENS(__probe_desc___, TRACEPOINT_PROVIDER));
530 assert(!ret);
531}
532
533static void __attribute__((destructor))
534_TP_COMBINE_TOKENS(__lttng_events_exit__, TRACEPOINT_PROVIDER)(void)
535{
536 ltt_probe_unregister(&_TP_COMBINE_TOKENS(__probe_desc___, TRACEPOINT_PROVIDER));
537}
628e1d81
MD
538
539int _TP_COMBINE_TOKENS(__tracepoint_provider_, TRACEPOINT_PROVIDER);
This page took 0.048885 seconds and 4 git commands to generate.