TRACEPOINT_EVENT: add floating point support
[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 <ust/lttng-events.h>
22
23 /*
24 * Macro declarations used for all stages.
25 */
26
27 #undef ctf_integer
28 #define ctf_integer(_type, _item, _src) \
29 ctf_integer_ext(_type, _item, _src, BYTE_ORDER, 10)
30
31 #undef ctf_integer_hex
32 #define ctf_integer_hex(_type, _item, _src) \
33 ctf_integer_ext(_type, _item, _src, BYTE_ORDER, 16)
34
35 #undef ctf_integer_network
36 #define ctf_integer_network(_type, _item, _src) \
37 ctf_integer_ext(_type, _item, _src, BIG_ENDIAN, 10)
38
39 #undef ctf_integer_network_hex
40 #define ctf_integer_network_hex(_type, _item, _src) \
41 ctf_integer_ext(_type, _item, _src, BIG_ENDIAN, 16)
42
43 #undef ctf_array
44 #define ctf_array(_type, _item, _src, _length) \
45 ctf_array_encoded(_type, _item, _src, _length, none)
46
47 #undef ctf_array_text
48 #define ctf_array_text(_type, _item, _src, _length) \
49 ctf_array_encoded(_type, _item, _src, _length, UTF8)
50
51 #undef ctf_sequence
52 #define ctf_sequence(_type, _item, _src, _length_type, _src_length) \
53 ctf_sequence_encoded(_type, _item, _src, \
54 _length_type, _src_length, none)
55
56 #undef ctf_sequence_text
57 #define ctf_sequence_text(_type, _item, _src, _length_type, _src_length) \
58 ctf_sequence_encoded(_type, _item, _src, \
59 _length_type, _src_length, UTF8)
60
61 /* ctf_string is redefined at each step */
62
63 /*
64 * TRACEPOINT_EVENT_CLASS can be used to add a generic function handlers
65 * for events. That is, if all events have the same parameters and just
66 * have distinct trace points. Each tracepoint can be defined with
67 * TRACEPOINT_EVENT_INSTANCE and that will map the
68 * TRACEPOINT_EVENT_CLASS to the tracepoint.
69 *
70 * TRACEPOINT_EVENT is a one to one mapping between tracepoint and
71 * template.
72 */
73
74 #undef TRACEPOINT_EVENT
75 #define TRACEPOINT_EVENT(name, proto, args, fields) \
76 TRACEPOINT_EVENT_CLASS(name, \
77 TP_PARAMS(proto), \
78 TP_PARAMS(args), \
79 TP_PARAMS(fields)) \
80 TRACEPOINT_EVENT_INSTANCE(name, name, TP_PARAMS(proto), TP_PARAMS(args))
81
82 /*
83 * Stage 1 of the trace events.
84 *
85 * Create event field type metadata section.
86 * Each event produce an array of fields.
87 */
88
89 /* Reset all macros within TRACE_EVENT */
90 #include "lttng-tracepoint-events-reset.h"
91
92 /* Named field types must be defined in lttng-types.h */
93
94 #undef ctf_integer_ext
95 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
96 { \
97 .name = #_item, \
98 .type = __type_integer(_type, _byte_order, _base, none),\
99 },
100
101 #undef ctf_float
102 #define ctf_float(_type, _item, _src) \
103 { \
104 .name = #_item, \
105 .type = __type_float(_type), \
106 },
107
108 #undef ctf_array_encoded
109 #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \
110 { \
111 .name = #_item, \
112 .type = \
113 { \
114 .atype = atype_array, \
115 .u.array = \
116 { \
117 .length = _length, \
118 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
119 }, \
120 }, \
121 },
122
123 #undef ctf_sequence_encoded
124 #define ctf_sequence_encoded(_type, _item, _src, \
125 _length_type, _src_length, _encoding) \
126 { \
127 .name = #_item, \
128 .type = \
129 { \
130 .atype = atype_sequence, \
131 .u.sequence = \
132 { \
133 .length_type = __type_integer(_length_type, BYTE_ORDER, 10, none), \
134 .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \
135 }, \
136 }, \
137 },
138
139 #undef ctf_string
140 #define ctf_string(_item, _src) \
141 { \
142 .name = #_item, \
143 .type = \
144 { \
145 .atype = atype_string, \
146 .u.basic.string.encoding = lttng_encode_UTF8, \
147 }, \
148 },
149
150 #undef TP_FIELDS
151 #define TP_FIELDS(args...) args /* Only one used in this phase */
152
153 #undef TRACEPOINT_EVENT_CLASS
154 #define TRACEPOINT_EVENT_CLASS(_name, _proto, _args, _fields) \
155 static const struct lttng_event_field __event_fields___##_name[] = { \
156 _fields \
157 };
158
159 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
160
161 /*
162 * Stage 2 of the trace events.
163 *
164 * Create probe callback prototypes.
165 */
166
167 /* Reset all macros within TRACE_EVENT */
168 #include "lttng-tracepoint-events-reset.h"
169
170 #undef TP_PROTO
171 #define TP_PROTO(args...) args
172
173 #undef TRACEPOINT_EVENT_CLASS
174 #define TRACEPOINT_EVENT_CLASS(_name, _proto, _args, _fields) \
175 static void __event_probe__##_name(void *__data, _proto);
176
177 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
178
179 /*
180 * Stage 3 of the trace events.
181 *
182 * Create an array of events.
183 */
184
185 /* Named field types must be defined in lttng-types.h */
186
187 /* Reset all macros within TRACE_EVENT */
188 #include "lttng-tracepoint-events-reset.h"
189
190 #undef TRACEPOINT_EVENT_INSTANCE
191 #define TRACEPOINT_EVENT_INSTANCE(_template, _name, _proto, _args) \
192 { \
193 .fields = __event_fields___##_template, \
194 .name = #_name, \
195 .probe_callback = (void *) &__event_probe__##_template,\
196 .nr_fields = ARRAY_SIZE(__event_fields___##_template), \
197 },
198
199 #define TP_ID1(_token, _system) _token##_system
200 #define TP_ID(_token, _system) TP_ID1(_token, _system)
201
202 static const struct lttng_event_desc TP_ID(__event_desc___, TRACE_SYSTEM)[] = {
203 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
204 };
205
206 #undef TP_ID1
207 #undef TP_ID
208
209
210 /*
211 * Stage 4 of the trace events.
212 *
213 * Create a toplevel descriptor for the whole probe.
214 */
215
216 #define TP_ID1(_token, _system) _token##_system
217 #define TP_ID(_token, _system) TP_ID1(_token, _system)
218
219 /* non-const because list head will be modified when registered. */
220 static struct lttng_probe_desc TP_ID(__probe_desc___, TRACE_SYSTEM) = {
221 .event_desc = TP_ID(__event_desc___, TRACE_SYSTEM),
222 .nr_events = ARRAY_SIZE(TP_ID(__event_desc___, TRACE_SYSTEM)),
223 };
224
225 #undef TP_ID1
226 #undef TP_ID
227
228 /*
229 * Stage 5 of the trace events.
230 *
231 * Create static inline function that calculates event size.
232 */
233
234 /* Reset all macros within TRACE_EVENT */
235 #include "lttng-tracepoint-events-reset.h"
236
237 /* Named field types must be defined in lttng-types.h */
238
239 #undef ctf_integer_ext
240 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
241 __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(_type)); \
242 __event_len += sizeof(_type);
243
244 #undef ctf_float
245 #define ctf_float(_type, _item, _src) \
246 __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(_type)); \
247 __event_len += sizeof(_type);
248
249 #undef ctf_array_encoded
250 #define ctf_array_encoded(_type, _item, _src, _length) \
251 __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(_type)); \
252 __event_len += sizeof(_type) * (_length);
253
254 #undef ctf_sequence_encoded
255 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
256 _src_length, _encoding) \
257 __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(_length_type)); \
258 __event_len += sizeof(_length_type); \
259 __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(_type)); \
260 __dynamic_len[__dynamic_len_idx] = (_length); \
261 __event_len += sizeof(_type) * __dynamic_len[__dynamic_len_idx]; \
262 __dynamic_len_idx++;
263
264 #undef ctf_string
265 #define ctf_string(_item, _src) \
266 __event_len += __dynamic_len[__dynamic_len_idx++] = strlen(_src) + 1;
267
268 #undef TP_PROTO
269 #define TP_PROTO(args...) args
270
271 #undef TP_FIELDS
272 #define TP_FIELDS(args...) args
273
274 #undef TRACEPOINT_EVENT_CLASS
275 #define TRACEPOINT_EVENT_CLASS(_name, _proto, _args, _fields) \
276 static inline size_t __event_get_size__##_name(size_t *__dynamic_len, _proto) \
277 { \
278 size_t __event_len = 0; \
279 unsigned int __dynamic_len_idx = 0; \
280 \
281 if (0) \
282 (void) __dynamic_len_idx; /* don't warn if unused */ \
283 _fields \
284 return __event_len; \
285 }
286
287 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
288
289 /*
290 * Stage 6 of the trace events.
291 *
292 * Create static inline function that calculates event payload alignment.
293 */
294
295 /* Reset all macros within TRACE_EVENT */
296 #include "lttng-tracepoint-events-reset.h"
297
298 /* Named field types must be defined in lttng-types.h */
299
300 #undef ctf_integer_ext
301 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
302 __event_align = max_t(size_t, __event_align, ltt_alignof(_type));
303
304 #undef ctf_float
305 #define ctf_float(_type, _item, _src) \
306 __event_align = max_t(size_t, __event_align, ltt_alignof(_type));
307
308 #undef ctf_array_encoded
309 #define ctf_array_encoded(_type, _item, _src, _length) \
310 __event_align = max_t(size_t, __event_align, ltt_alignof(_type));
311
312 #undef ctf_sequence_encoded
313 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
314 _src_length, _encoding) \
315 __event_align = max_t(size_t, __event_align, ltt_alignof(_length_type)); \
316 __event_align = max_t(size_t, __event_align, ltt_alignof(_type));
317
318 #undef ctf_string
319 #define ctf_string(_item, _src)
320
321 #undef TP_PROTO
322 #define TP_PROTO(args...) args
323
324 #undef TP_FIELDS
325 #define TP_FIELDS(args...) args
326
327 #undef TRACEPOINT_EVENT_CLASS
328 #define TRACEPOINT_EVENT_CLASS(_name, _proto, _args, _fields) \
329 static inline size_t __event_get_align__##_name(_proto) \
330 { \
331 size_t __event_align = 1; \
332 _fields \
333 return __event_align; \
334 }
335
336 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
337
338
339 /*
340 * Stage 7 of the trace events.
341 *
342 * Create the probe function : call even size calculation and write event data
343 * into the buffer.
344 *
345 * We use both the field and assignment macros to write the fields in the order
346 * defined in the field declaration. The field declarations control the
347 * execution order, jumping to the appropriate assignment block.
348 */
349
350 /* Reset all macros within TRACE_EVENT */
351 #include "lttng-tracepoint-events-reset.h"
352
353 #undef ctf_integer_ext
354 #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \
355 { \
356 _type __tmp = (_src); \
357 lib_ring_buffer_align_ctx(&ctx, ltt_alignof(__tmp)); \
358 __chan->ops->event_write(&ctx, &__tmp, sizeof(__tmp)); \
359 }
360
361 #undef ctf_float
362 #define ctf_float(_type, _item, _src) \
363 { \
364 _type __tmp = (_src); \
365 lib_ring_buffer_align_ctx(&ctx, ltt_alignof(__tmp)); \
366 __chan->ops->event_write(&ctx, &__tmp, sizeof(__tmp)); \
367 }
368
369 #undef ctf_array_encoded
370 #define ctf_array_encoded(_type, _item, _src, _length) \
371 lib_ring_buffer_align_ctx(&ctx, ltt_alignof(_type)); \
372 __chan->ops->event_write(&ctx, _src, _length);
373
374 #undef ctf_sequence_encoded
375 #define ctf_sequence_encoded(_type, _item, _src, _length_type, \
376 _src_length, _encoding) \
377 { \
378 _length_type __tmpl = __dynamic_len[__dynamic_len_idx]; \
379 lib_ring_buffer_align_ctx(&ctx, ltt_alignof(_length_type)); \
380 __chan->ops->event_write(&ctx, &__tmpl, sizeof(_length_type)); \
381 } \
382 lib_ring_buffer_align_ctx(&ctx, ltt_alignof(_type)); \
383 __chan->ops->event_write(&ctx, _src, \
384 sizeof(_type) * __get_sequence_len(dest));
385
386 #undef ctf_string
387 #define ctf_string(_item, _src) \
388 tp_memcpy(dest, _src, __get_sequence_len(dest))
389
390 /* Beware: this get len actually consumes the len value */
391 #undef __get_sequence_len
392 #define __get_sequence_len(field) __dynamic_len[__dynamic_len_idx++]
393
394 #undef TP_PROTO
395 #define TP_PROTO(args...) args
396
397 #undef TP_ARGS
398 #define TP_ARGS(args...) args
399
400 #undef TP_FIELDS
401 #define TP_FIELDS(args...) args
402
403 #undef TRACEPOINT_EVENT_CLASS
404 #define TRACEPOINT_EVENT_CLASS(_name, _proto, _args, _fields) \
405 static void __event_probe__##_name(void *__data, _proto) \
406 { \
407 struct ltt_event *__event = __data; \
408 struct ltt_channel *__chan = __event->chan; \
409 struct lib_ring_buffer_ctx ctx; \
410 size_t __event_len, __event_align; \
411 size_t __dynamic_len_idx = 0; \
412 size_t __dynamic_len[ARRAY_SIZE(__event_fields___##_name)]; \
413 int __ret; \
414 \
415 if (0) \
416 (void) __dynamic_len_idx; /* don't warn if unused */ \
417 if (unlikely(!ACCESS_ONCE(__chan->session->active))) \
418 return; \
419 if (unlikely(!ACCESS_ONCE(__chan->enabled))) \
420 return; \
421 if (unlikely(!ACCESS_ONCE(__event->enabled))) \
422 return; \
423 __event_len = __event_get_size__##_name(__dynamic_len, _args); \
424 __event_align = __event_get_align__##_name(_args); \
425 lib_ring_buffer_ctx_init(&ctx, __chan->chan, __event, __event_len, \
426 __event_align, -1); \
427 __ret = __chan->ops->event_reserve(&ctx, __event->id); \
428 if (__ret < 0) \
429 return; \
430 _fields \
431 __chan->ops->event_commit(&ctx); \
432 }
433
434 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
435
436 /*
437 * Stage 8 of the trace events.
438 *
439 * Register/unregister probes at module load/unload.
440 */
441
442 /* Reset all macros within TRACE_EVENT */
443 #include "lttng-tracepoint-events-reset.h"
444
445 #define TP_ID1(_token, _system) _token##_system
446 #define TP_ID(_token, _system) TP_ID1(_token, _system)
447
448 static void __attribute__((constructor))
449 TP_ID(__lttng_events_init__, TRACE_SYSTEM)(void)
450 {
451 int ret;
452
453 ret = ltt_probe_register(&TP_ID(__probe_desc___, TRACE_SYSTEM));
454 assert(!ret);
455 }
456
457 static void __attribute__((destructor))
458 TP_ID(__lttng_events_exit__, TRACE_SYSTEM)(void)
459 {
460 ltt_probe_unregister(&TP_ID(__probe_desc___, TRACE_SYSTEM));
461 }
462
463 #undef TP_ID1
464 #undef TP_ID
This page took 0.037953 seconds and 4 git commands to generate.