test and fix array, sequence, string
[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
46#undef ctf_array
47#define ctf_array(_type, _item, _src, _length) \
48 ctf_array_encoded(_type, _item, _src, _length, none)
49
50#undef ctf_array_text
51#define ctf_array_text(_type, _item, _src, _length) \
52 ctf_array_encoded(_type, _item, _src, _length, UTF8)
53
54#undef ctf_sequence
55#define ctf_sequence(_type, _item, _src, _length_type, _src_length) \
56 ctf_sequence_encoded(_type, _item, _src, \
57 _length_type, _src_length, none)
58
59#undef ctf_sequence_text
60#define ctf_sequence_text(_type, _item, _src, _length_type, _src_length) \
61 ctf_sequence_encoded(_type, _item, _src, \
62 _length_type, _src_length, UTF8)
63
64/* ctf_string is redefined at each step */
65
66/*
67 * TRACEPOINT_EVENT_CLASS can be used to add a generic function handlers
68 * for events. That is, if all events have the same parameters and just
69 * have distinct trace points. Each tracepoint can be defined with
70 * TRACEPOINT_EVENT_INSTANCE and that will map the
71 * TRACEPOINT_EVENT_CLASS to the tracepoint.
72 *
73 * TRACEPOINT_EVENT is a one to one mapping between tracepoint and
74 * template.
75 */
76
77#undef TRACEPOINT_EVENT
78#define TRACEPOINT_EVENT(name, proto, args, fields) \
79 TRACEPOINT_EVENT_CLASS(name, \
80 TP_PARAMS(proto), \
81 TP_PARAMS(args), \
82 TP_PARAMS(fields)) \
83 TRACEPOINT_EVENT_INSTANCE(name, name, TP_PARAMS(proto), TP_PARAMS(args))
84
8d8a24c8
MD
85#undef TRACEPOINT_EVENT_NOARGS
86#define TRACEPOINT_EVENT_NOARGS(name, fields) \
87 TRACEPOINT_EVENT_CLASS_NOARGS(name, \
88 TP_PARAMS(fields)) \
89 TRACEPOINT_EVENT_INSTANCE_NOARGS(name, name)
90
91/* Helpers */
92#define _TP_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
93
1dbfff0c
MD
94#define _tp_max_t(type, x, y) \
95 ({ \
96 type __max1 = (x); \
97 type __max2 = (y); \
98 __max1 > __max2 ? __max1: __max2; \
99 })
100
101
0a42beb6
MD
102/*
103 * Stage 1 of the trace events.
104 *
105 * Create event field type metadata section.
106 * Each event produce an array of fields.
107 */
108
8d8a24c8
MD
109/* Reset all macros within TRACEPOINT_EVENT */
110#include <ust/lttng-tracepoint-event-reset.h>
0a42beb6
MD
111
112/* Named field types must be defined in lttng-types.h */
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
MD
191/* Reset all macros within TRACEPOINT_EVENT */
192#include <ust/lttng-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
213/* Named field types must be defined in lttng-types.h */
214
8d8a24c8
MD
215/* Reset all macros within TRACEPOINT_EVENT */
216#include <ust/lttng-tracepoint-event-reset.h>
0a42beb6 217
8d8a24c8
MD
218#undef TRACEPOINT_EVENT_INSTANCE_NOARGS
219#define TRACEPOINT_EVENT_INSTANCE_NOARGS(_template, _name) \
0a42beb6
MD
220 { \
221 .fields = __event_fields___##_template, \
222 .name = #_name, \
223 .probe_callback = (void *) &__event_probe__##_template,\
8d8a24c8 224 .nr_fields = _TP_ARRAY_SIZE(__event_fields___##_template), \
0a42beb6
MD
225 },
226
8d8a24c8
MD
227#undef TRACEPOINT_EVENT_INSTANCE
228#define TRACEPOINT_EVENT_INSTANCE(_template, _name, _proto, _args) \
229 TRACEPOINT_EVENT_INSTANCE_NOARGS(_template, _name)
230
0a42beb6
MD
231#define TP_ID1(_token, _system) _token##_system
232#define TP_ID(_token, _system) TP_ID1(_token, _system)
233
8d8a24c8
MD
234static const struct lttng_event_desc TP_ID(__event_desc___, TRACEPOINT_SYSTEM)[] = {
235#include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
0a42beb6
MD
236};
237
238#undef TP_ID1
239#undef TP_ID
240
241
242/*
243 * Stage 4 of the trace events.
244 *
245 * Create a toplevel descriptor for the whole probe.
246 */
247
248#define TP_ID1(_token, _system) _token##_system
249#define TP_ID(_token, _system) TP_ID1(_token, _system)
250
251/* non-const because list head will be modified when registered. */
8d8a24c8
MD
252static struct lttng_probe_desc TP_ID(__probe_desc___, TRACEPOINT_SYSTEM) = {
253 .event_desc = TP_ID(__event_desc___, TRACEPOINT_SYSTEM),
254 .nr_events = _TP_ARRAY_SIZE(TP_ID(__event_desc___, TRACEPOINT_SYSTEM)),
0a42beb6
MD
255};
256
257#undef TP_ID1
258#undef TP_ID
259
260/*
261 * Stage 5 of the trace events.
262 *
263 * Create static inline function that calculates event size.
264 */
265
8d8a24c8
MD
266/* Reset all macros within TRACEPOINT_EVENT */
267#include <ust/lttng-tracepoint-event-reset.h>
0a42beb6
MD
268
269/* Named field types must be defined in lttng-types.h */
270
271#undef ctf_integer_ext
272#define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
1dbfff0c 273 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
0a42beb6
MD
274 __event_len += sizeof(_type);
275
20f1eee7
MD
276#undef ctf_float
277#define ctf_float(_type, _item, _src) \
1dbfff0c 278 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
20f1eee7
MD
279 __event_len += sizeof(_type);
280
0a42beb6 281#undef ctf_array_encoded
41aaf8a5 282#define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
1dbfff0c 283 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
0a42beb6
MD
284 __event_len += sizeof(_type) * (_length);
285
286#undef ctf_sequence_encoded
287#define ctf_sequence_encoded(_type, _item, _src, _length_type, \
288 _src_length, _encoding) \
1dbfff0c 289 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_length_type)); \
0a42beb6 290 __event_len += sizeof(_length_type); \
1dbfff0c 291 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
5dba5937 292 __dynamic_len[__dynamic_len_idx] = (_src_length); \
0a42beb6
MD
293 __event_len += sizeof(_type) * __dynamic_len[__dynamic_len_idx]; \
294 __dynamic_len_idx++;
295
296#undef ctf_string
297#define ctf_string(_item, _src) \
298 __event_len += __dynamic_len[__dynamic_len_idx++] = strlen(_src) + 1;
299
300#undef TP_PROTO
301#define TP_PROTO(args...) args
302
303#undef TP_FIELDS
304#define TP_FIELDS(args...) args
305
306#undef TRACEPOINT_EVENT_CLASS
307#define TRACEPOINT_EVENT_CLASS(_name, _proto, _args, _fields) \
308static inline size_t __event_get_size__##_name(size_t *__dynamic_len, _proto) \
309{ \
310 size_t __event_len = 0; \
311 unsigned int __dynamic_len_idx = 0; \
312 \
313 if (0) \
314 (void) __dynamic_len_idx; /* don't warn if unused */ \
315 _fields \
316 return __event_len; \
317}
318
8d8a24c8
MD
319#undef TRACEPOINT_EVENT_CLASS_NOARGS
320#define TRACEPOINT_EVENT_CLASS_NOARGS(_name, _fields) \
321static inline size_t __event_get_size__##_name(size_t *__dynamic_len) \
322{ \
323 size_t __event_len = 0; \
324 unsigned int __dynamic_len_idx = 0; \
325 \
326 if (0) \
327 (void) __dynamic_len_idx; /* don't warn if unused */ \
328 _fields \
329 return __event_len; \
330}
331
332#include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
0a42beb6
MD
333
334/*
335 * Stage 6 of the trace events.
336 *
337 * Create static inline function that calculates event payload alignment.
338 */
339
8d8a24c8
MD
340/* Reset all macros within TRACEPOINT_EVENT */
341#include <ust/lttng-tracepoint-event-reset.h>
0a42beb6
MD
342
343/* Named field types must be defined in lttng-types.h */
344
345#undef ctf_integer_ext
346#define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
1dbfff0c 347 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
0a42beb6 348
20f1eee7
MD
349#undef ctf_float
350#define ctf_float(_type, _item, _src) \
1dbfff0c 351 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
20f1eee7 352
0a42beb6 353#undef ctf_array_encoded
41aaf8a5 354#define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
1dbfff0c 355 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
0a42beb6
MD
356
357#undef ctf_sequence_encoded
358#define ctf_sequence_encoded(_type, _item, _src, _length_type, \
359 _src_length, _encoding) \
1dbfff0c
MD
360 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_length_type)); \
361 __event_align = _tp_max_t(size_t, __event_align, lttng_alignof(_type));
0a42beb6
MD
362
363#undef ctf_string
364#define ctf_string(_item, _src)
365
366#undef TP_PROTO
367#define TP_PROTO(args...) args
368
369#undef TP_FIELDS
370#define TP_FIELDS(args...) args
371
372#undef TRACEPOINT_EVENT_CLASS
373#define TRACEPOINT_EVENT_CLASS(_name, _proto, _args, _fields) \
374static inline size_t __event_get_align__##_name(_proto) \
375{ \
376 size_t __event_align = 1; \
377 _fields \
378 return __event_align; \
379}
380
8d8a24c8
MD
381#undef TRACEPOINT_EVENT_CLASS_NOARGS
382#define TRACEPOINT_EVENT_CLASS_NOARGS(_name, _fields) \
383static inline size_t __event_get_align__##_name(void) \
384{ \
385 size_t __event_align = 1; \
386 _fields \
387 return __event_align; \
388}
389
390#include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
0a42beb6
MD
391
392
393/*
394 * Stage 7 of the trace events.
395 *
396 * Create the probe function : call even size calculation and write event data
397 * into the buffer.
398 *
399 * We use both the field and assignment macros to write the fields in the order
400 * defined in the field declaration. The field declarations control the
401 * execution order, jumping to the appropriate assignment block.
402 */
403
8d8a24c8
MD
404/* Reset all macros within TRACEPOINT_EVENT */
405#include <ust/lttng-tracepoint-event-reset.h>
0a42beb6
MD
406
407#undef ctf_integer_ext
408#define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
409 { \
410 _type __tmp = (_src); \
1dbfff0c 411 lib_ring_buffer_align_ctx(&ctx, lttng_alignof(__tmp)); \
0a42beb6
MD
412 __chan->ops->event_write(&ctx, &__tmp, sizeof(__tmp)); \
413 }
414
20f1eee7
MD
415#undef ctf_float
416#define ctf_float(_type, _item, _src) \
417 { \
418 _type __tmp = (_src); \
1dbfff0c 419 lib_ring_buffer_align_ctx(&ctx, lttng_alignof(__tmp)); \
20f1eee7
MD
420 __chan->ops->event_write(&ctx, &__tmp, sizeof(__tmp)); \
421 }
422
0a42beb6 423#undef ctf_array_encoded
41aaf8a5 424#define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
1dbfff0c 425 lib_ring_buffer_align_ctx(&ctx, lttng_alignof(_type)); \
0a42beb6
MD
426 __chan->ops->event_write(&ctx, _src, _length);
427
428#undef ctf_sequence_encoded
429#define ctf_sequence_encoded(_type, _item, _src, _length_type, \
430 _src_length, _encoding) \
431 { \
432 _length_type __tmpl = __dynamic_len[__dynamic_len_idx]; \
5dba5937 433 lib_ring_buffer_align_ctx(&ctx, lttng_alignof(_length_type)); \
0a42beb6
MD
434 __chan->ops->event_write(&ctx, &__tmpl, sizeof(_length_type)); \
435 } \
1dbfff0c 436 lib_ring_buffer_align_ctx(&ctx, lttng_alignof(_type)); \
0a42beb6 437 __chan->ops->event_write(&ctx, _src, \
5dba5937 438 sizeof(_type) * __get_dynamic_len(dest));
0a42beb6
MD
439
440#undef ctf_string
441#define ctf_string(_item, _src) \
5dba5937
MD
442 lib_ring_buffer_align_ctx(&ctx, lttng_alignof(*(_src))); \
443 __chan->ops->event_write(&ctx, _src, __get_dynamic_len(dest));
0a42beb6
MD
444
445/* Beware: this get len actually consumes the len value */
5dba5937
MD
446#undef __get_dynamic_len
447#define __get_dynamic_len(field) __dynamic_len[__dynamic_len_idx++]
0a42beb6
MD
448
449#undef TP_PROTO
450#define TP_PROTO(args...) args
451
452#undef TP_ARGS
453#define TP_ARGS(args...) args
454
455#undef TP_FIELDS
456#define TP_FIELDS(args...) args
457
458#undef TRACEPOINT_EVENT_CLASS
459#define TRACEPOINT_EVENT_CLASS(_name, _proto, _args, _fields) \
460static void __event_probe__##_name(void *__data, _proto) \
461{ \
462 struct ltt_event *__event = __data; \
463 struct ltt_channel *__chan = __event->chan; \
464 struct lib_ring_buffer_ctx ctx; \
465 size_t __event_len, __event_align; \
466 size_t __dynamic_len_idx = 0; \
8d8a24c8 467 size_t __dynamic_len[_TP_ARRAY_SIZE(__event_fields___##_name)]; \
0a42beb6
MD
468 int __ret; \
469 \
470 if (0) \
471 (void) __dynamic_len_idx; /* don't warn if unused */ \
8d8a24c8 472 if (unlikely(!CMM_ACCESS_ONCE(__chan->session->active))) \
0a42beb6 473 return; \
8d8a24c8 474 if (unlikely(!CMM_ACCESS_ONCE(__chan->enabled))) \
0a42beb6 475 return; \
8d8a24c8 476 if (unlikely(!CMM_ACCESS_ONCE(__event->enabled))) \
0a42beb6
MD
477 return; \
478 __event_len = __event_get_size__##_name(__dynamic_len, _args); \
479 __event_align = __event_get_align__##_name(_args); \
480 lib_ring_buffer_ctx_init(&ctx, __chan->chan, __event, __event_len, \
481 __event_align, -1); \
482 __ret = __chan->ops->event_reserve(&ctx, __event->id); \
483 if (__ret < 0) \
484 return; \
485 _fields \
486 __chan->ops->event_commit(&ctx); \
487}
488
8d8a24c8
MD
489#undef TRACEPOINT_EVENT_CLASS_NOARGS
490#define TRACEPOINT_EVENT_CLASS_NOARGS(_name, _fields) \
491static void __event_probe__##_name(void *__data) \
492{ \
493 struct ltt_event *__event = __data; \
494 struct ltt_channel *__chan = __event->chan; \
495 struct lib_ring_buffer_ctx ctx; \
496 size_t __event_len, __event_align; \
497 size_t __dynamic_len_idx = 0; \
498 size_t __dynamic_len[_TP_ARRAY_SIZE(__event_fields___##_name)]; \
499 int __ret; \
500 \
501 if (0) \
502 (void) __dynamic_len_idx; /* don't warn if unused */ \
503 if (unlikely(!CMM_ACCESS_ONCE(__chan->session->active))) \
504 return; \
505 if (unlikely(!CMM_ACCESS_ONCE(__chan->enabled))) \
506 return; \
507 if (unlikely(!CMM_ACCESS_ONCE(__event->enabled))) \
508 return; \
509 __event_len = __event_get_size__##_name(__dynamic_len); \
510 __event_align = __event_get_align__##_name(); \
511 lib_ring_buffer_ctx_init(&ctx, __chan->chan, __event, __event_len, \
512 __event_align, -1); \
513 __ret = __chan->ops->event_reserve(&ctx, __event->id); \
514 if (__ret < 0) \
515 return; \
516 _fields \
517 __chan->ops->event_commit(&ctx); \
518}
519
520#include TRACEPOINT_INCLUDE(TRACEPOINT_INCLUDE_FILE)
0a42beb6 521
5dba5937
MD
522#undef __get_dynamic_len
523
0a42beb6
MD
524/*
525 * Stage 8 of the trace events.
526 *
527 * Register/unregister probes at module load/unload.
528 */
529
8d8a24c8
MD
530/* Reset all macros within TRACEPOINT_EVENT */
531#include <ust/lttng-tracepoint-event-reset.h>
0a42beb6
MD
532
533#define TP_ID1(_token, _system) _token##_system
534#define TP_ID(_token, _system) TP_ID1(_token, _system)
535
536static void __attribute__((constructor))
8d8a24c8 537TP_ID(__lttng_events_init__, TRACEPOINT_SYSTEM)(void)
0a42beb6
MD
538{
539 int ret;
540
8d8a24c8 541 ret = ltt_probe_register(&TP_ID(__probe_desc___, TRACEPOINT_SYSTEM));
0a42beb6
MD
542 assert(!ret);
543}
544
545static void __attribute__((destructor))
8d8a24c8 546TP_ID(__lttng_events_exit__, TRACEPOINT_SYSTEM)(void)
0a42beb6 547{
8d8a24c8 548 ltt_probe_unregister(&TP_ID(__probe_desc___, TRACEPOINT_SYSTEM));
0a42beb6
MD
549}
550
551#undef TP_ID1
552#undef TP_ID
This page took 0.047655 seconds and 4 git commands to generate.