Commit | Line | Data |
---|---|---|
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> | |
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_array_encoded | |
102 | #define ctf_array_encoded(_type, _item, _src, _length, _encoding) \ | |
103 | { \ | |
104 | .name = #_item, \ | |
105 | .type = \ | |
106 | { \ | |
107 | .atype = atype_array, \ | |
108 | .u.array = \ | |
109 | { \ | |
110 | .length = _length, \ | |
111 | .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \ | |
112 | }, \ | |
113 | }, \ | |
114 | }, | |
115 | ||
116 | #undef ctf_sequence_encoded | |
117 | #define ctf_sequence_encoded(_type, _item, _src, \ | |
118 | _length_type, _src_length, _encoding) \ | |
119 | { \ | |
120 | .name = #_item, \ | |
121 | .type = \ | |
122 | { \ | |
123 | .atype = atype_sequence, \ | |
124 | .u.sequence = \ | |
125 | { \ | |
126 | .length_type = __type_integer(_length_type, BYTE_ORDER, 10, none), \ | |
127 | .elem_type = __type_integer(_type, BYTE_ORDER, 10, _encoding), \ | |
128 | }, \ | |
129 | }, \ | |
130 | }, | |
131 | ||
132 | #undef ctf_string | |
133 | #define ctf_string(_item, _src) \ | |
134 | { \ | |
135 | .name = #_item, \ | |
136 | .type = \ | |
137 | { \ | |
138 | .atype = atype_string, \ | |
139 | .u.basic.string.encoding = lttng_encode_UTF8, \ | |
140 | }, \ | |
141 | }, | |
142 | ||
143 | #undef TP_FIELDS | |
144 | #define TP_FIELDS(args...) args /* Only one used in this phase */ | |
145 | ||
146 | #undef TRACEPOINT_EVENT_CLASS | |
147 | #define TRACEPOINT_EVENT_CLASS(_name, _proto, _args, _fields) \ | |
148 | static const struct lttng_event_field __event_fields___##_name[] = { \ | |
149 | _fields \ | |
150 | }; | |
151 | ||
152 | #include TRACE_INCLUDE(TRACE_INCLUDE_FILE) | |
153 | ||
154 | /* | |
155 | * Stage 2 of the trace events. | |
156 | * | |
157 | * Create probe callback prototypes. | |
158 | */ | |
159 | ||
160 | /* Reset all macros within TRACE_EVENT */ | |
161 | #include "lttng-tracepoint-events-reset.h" | |
162 | ||
163 | #undef TP_PROTO | |
164 | #define TP_PROTO(args...) args | |
165 | ||
166 | #undef TRACEPOINT_EVENT_CLASS | |
167 | #define TRACEPOINT_EVENT_CLASS(_name, _proto, _args, _fields) \ | |
168 | static void __event_probe__##_name(void *__data, _proto); | |
169 | ||
170 | #include TRACE_INCLUDE(TRACE_INCLUDE_FILE) | |
171 | ||
172 | /* | |
173 | * Stage 3 of the trace events. | |
174 | * | |
175 | * Create an array of events. | |
176 | */ | |
177 | ||
178 | /* Named field types must be defined in lttng-types.h */ | |
179 | ||
180 | /* Reset all macros within TRACE_EVENT */ | |
181 | #include "lttng-tracepoint-events-reset.h" | |
182 | ||
183 | #undef TRACEPOINT_EVENT_INSTANCE | |
184 | #define TRACEPOINT_EVENT_INSTANCE(_template, _name, _proto, _args) \ | |
185 | { \ | |
186 | .fields = __event_fields___##_template, \ | |
187 | .name = #_name, \ | |
188 | .probe_callback = (void *) &__event_probe__##_template,\ | |
189 | .nr_fields = ARRAY_SIZE(__event_fields___##_template), \ | |
190 | }, | |
191 | ||
192 | #define TP_ID1(_token, _system) _token##_system | |
193 | #define TP_ID(_token, _system) TP_ID1(_token, _system) | |
194 | ||
195 | static const struct lttng_event_desc TP_ID(__event_desc___, TRACE_SYSTEM)[] = { | |
196 | #include TRACE_INCLUDE(TRACE_INCLUDE_FILE) | |
197 | }; | |
198 | ||
199 | #undef TP_ID1 | |
200 | #undef TP_ID | |
201 | ||
202 | ||
203 | /* | |
204 | * Stage 4 of the trace events. | |
205 | * | |
206 | * Create a toplevel descriptor for the whole probe. | |
207 | */ | |
208 | ||
209 | #define TP_ID1(_token, _system) _token##_system | |
210 | #define TP_ID(_token, _system) TP_ID1(_token, _system) | |
211 | ||
212 | /* non-const because list head will be modified when registered. */ | |
213 | static struct lttng_probe_desc TP_ID(__probe_desc___, TRACE_SYSTEM) = { | |
214 | .event_desc = TP_ID(__event_desc___, TRACE_SYSTEM), | |
215 | .nr_events = ARRAY_SIZE(TP_ID(__event_desc___, TRACE_SYSTEM)), | |
216 | }; | |
217 | ||
218 | #undef TP_ID1 | |
219 | #undef TP_ID | |
220 | ||
221 | /* | |
222 | * Stage 5 of the trace events. | |
223 | * | |
224 | * Create static inline function that calculates event size. | |
225 | */ | |
226 | ||
227 | /* Reset all macros within TRACE_EVENT */ | |
228 | #include "lttng-tracepoint-events-reset.h" | |
229 | ||
230 | /* Named field types must be defined in lttng-types.h */ | |
231 | ||
232 | #undef ctf_integer_ext | |
233 | #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \ | |
234 | __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(_type)); \ | |
235 | __event_len += sizeof(_type); | |
236 | ||
237 | #undef ctf_array_encoded | |
238 | #define ctf_array_encoded(_type, _item, _src, _length) \ | |
239 | __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(_type)); \ | |
240 | __event_len += sizeof(_type) * (_length); | |
241 | ||
242 | #undef ctf_sequence_encoded | |
243 | #define ctf_sequence_encoded(_type, _item, _src, _length_type, \ | |
244 | _src_length, _encoding) \ | |
245 | __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(_length_type)); \ | |
246 | __event_len += sizeof(_length_type); \ | |
247 | __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(_type)); \ | |
248 | __dynamic_len[__dynamic_len_idx] = (_length); \ | |
249 | __event_len += sizeof(_type) * __dynamic_len[__dynamic_len_idx]; \ | |
250 | __dynamic_len_idx++; | |
251 | ||
252 | #undef ctf_string | |
253 | #define ctf_string(_item, _src) \ | |
254 | __event_len += __dynamic_len[__dynamic_len_idx++] = strlen(_src) + 1; | |
255 | ||
256 | #undef TP_PROTO | |
257 | #define TP_PROTO(args...) args | |
258 | ||
259 | #undef TP_FIELDS | |
260 | #define TP_FIELDS(args...) args | |
261 | ||
262 | #undef TRACEPOINT_EVENT_CLASS | |
263 | #define TRACEPOINT_EVENT_CLASS(_name, _proto, _args, _fields) \ | |
264 | static inline size_t __event_get_size__##_name(size_t *__dynamic_len, _proto) \ | |
265 | { \ | |
266 | size_t __event_len = 0; \ | |
267 | unsigned int __dynamic_len_idx = 0; \ | |
268 | \ | |
269 | if (0) \ | |
270 | (void) __dynamic_len_idx; /* don't warn if unused */ \ | |
271 | _fields \ | |
272 | return __event_len; \ | |
273 | } | |
274 | ||
275 | #include TRACE_INCLUDE(TRACE_INCLUDE_FILE) | |
276 | ||
277 | /* | |
278 | * Stage 6 of the trace events. | |
279 | * | |
280 | * Create static inline function that calculates event payload alignment. | |
281 | */ | |
282 | ||
283 | /* Reset all macros within TRACE_EVENT */ | |
284 | #include "lttng-tracepoint-events-reset.h" | |
285 | ||
286 | /* Named field types must be defined in lttng-types.h */ | |
287 | ||
288 | #undef ctf_integer_ext | |
289 | #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \ | |
290 | __event_align = max_t(size_t, __event_align, ltt_alignof(_type)); | |
291 | ||
292 | #undef ctf_array_encoded | |
293 | #define ctf_array_encoded(_type, _item, _src, _length) \ | |
294 | __event_align = max_t(size_t, __event_align, ltt_alignof(_type)); | |
295 | ||
296 | #undef ctf_sequence_encoded | |
297 | #define ctf_sequence_encoded(_type, _item, _src, _length_type, \ | |
298 | _src_length, _encoding) \ | |
299 | __event_align = max_t(size_t, __event_align, ltt_alignof(_length_type)); \ | |
300 | __event_align = max_t(size_t, __event_align, ltt_alignof(_type)); | |
301 | ||
302 | #undef ctf_string | |
303 | #define ctf_string(_item, _src) | |
304 | ||
305 | #undef TP_PROTO | |
306 | #define TP_PROTO(args...) args | |
307 | ||
308 | #undef TP_FIELDS | |
309 | #define TP_FIELDS(args...) args | |
310 | ||
311 | #undef TRACEPOINT_EVENT_CLASS | |
312 | #define TRACEPOINT_EVENT_CLASS(_name, _proto, _args, _fields) \ | |
313 | static inline size_t __event_get_align__##_name(_proto) \ | |
314 | { \ | |
315 | size_t __event_align = 1; \ | |
316 | _fields \ | |
317 | return __event_align; \ | |
318 | } | |
319 | ||
320 | #include TRACE_INCLUDE(TRACE_INCLUDE_FILE) | |
321 | ||
322 | ||
323 | /* | |
324 | * Stage 7 of the trace events. | |
325 | * | |
326 | * Create the probe function : call even size calculation and write event data | |
327 | * into the buffer. | |
328 | * | |
329 | * We use both the field and assignment macros to write the fields in the order | |
330 | * defined in the field declaration. The field declarations control the | |
331 | * execution order, jumping to the appropriate assignment block. | |
332 | */ | |
333 | ||
334 | /* Reset all macros within TRACE_EVENT */ | |
335 | #include "lttng-tracepoint-events-reset.h" | |
336 | ||
337 | #undef ctf_integer_ext | |
338 | #define ctf_integer_ext(_type, _item, _src, _byte_order, _base) \ | |
339 | { \ | |
340 | _type __tmp = (_src); \ | |
341 | lib_ring_buffer_align_ctx(&ctx, ltt_alignof(__tmp)); \ | |
342 | __chan->ops->event_write(&ctx, &__tmp, sizeof(__tmp)); \ | |
343 | } | |
344 | ||
345 | #undef ctf_array_encoded | |
346 | #define ctf_array_encoded(_type, _item, _src, _length) \ | |
347 | lib_ring_buffer_align_ctx(&ctx, ltt_alignof(_type)); \ | |
348 | __chan->ops->event_write(&ctx, _src, _length); | |
349 | ||
350 | #undef ctf_sequence_encoded | |
351 | #define ctf_sequence_encoded(_type, _item, _src, _length_type, \ | |
352 | _src_length, _encoding) \ | |
353 | { \ | |
354 | _length_type __tmpl = __dynamic_len[__dynamic_len_idx]; \ | |
355 | lib_ring_buffer_align_ctx(&ctx, ltt_alignof(_length_type)); \ | |
356 | __chan->ops->event_write(&ctx, &__tmpl, sizeof(_length_type)); \ | |
357 | } \ | |
358 | lib_ring_buffer_align_ctx(&ctx, ltt_alignof(_type)); \ | |
359 | __chan->ops->event_write(&ctx, _src, \ | |
360 | sizeof(_type) * __get_sequence_len(dest)); | |
361 | ||
362 | #undef ctf_string | |
363 | #define ctf_string(_item, _src) \ | |
364 | tp_memcpy(dest, _src, __get_sequence_len(dest)) | |
365 | ||
366 | /* Beware: this get len actually consumes the len value */ | |
367 | #undef __get_sequence_len | |
368 | #define __get_sequence_len(field) __dynamic_len[__dynamic_len_idx++] | |
369 | ||
370 | #undef TP_PROTO | |
371 | #define TP_PROTO(args...) args | |
372 | ||
373 | #undef TP_ARGS | |
374 | #define TP_ARGS(args...) args | |
375 | ||
376 | #undef TP_FIELDS | |
377 | #define TP_FIELDS(args...) args | |
378 | ||
379 | #undef TRACEPOINT_EVENT_CLASS | |
380 | #define TRACEPOINT_EVENT_CLASS(_name, _proto, _args, _fields) \ | |
381 | static void __event_probe__##_name(void *__data, _proto) \ | |
382 | { \ | |
383 | struct ltt_event *__event = __data; \ | |
384 | struct ltt_channel *__chan = __event->chan; \ | |
385 | struct lib_ring_buffer_ctx ctx; \ | |
386 | size_t __event_len, __event_align; \ | |
387 | size_t __dynamic_len_idx = 0; \ | |
388 | size_t __dynamic_len[ARRAY_SIZE(__event_fields___##_name)]; \ | |
389 | int __ret; \ | |
390 | \ | |
391 | if (0) \ | |
392 | (void) __dynamic_len_idx; /* don't warn if unused */ \ | |
393 | if (unlikely(!ACCESS_ONCE(__chan->session->active))) \ | |
394 | return; \ | |
395 | if (unlikely(!ACCESS_ONCE(__chan->enabled))) \ | |
396 | return; \ | |
397 | if (unlikely(!ACCESS_ONCE(__event->enabled))) \ | |
398 | return; \ | |
399 | __event_len = __event_get_size__##_name(__dynamic_len, _args); \ | |
400 | __event_align = __event_get_align__##_name(_args); \ | |
401 | lib_ring_buffer_ctx_init(&ctx, __chan->chan, __event, __event_len, \ | |
402 | __event_align, -1); \ | |
403 | __ret = __chan->ops->event_reserve(&ctx, __event->id); \ | |
404 | if (__ret < 0) \ | |
405 | return; \ | |
406 | _fields \ | |
407 | __chan->ops->event_commit(&ctx); \ | |
408 | } | |
409 | ||
410 | #include TRACE_INCLUDE(TRACE_INCLUDE_FILE) | |
411 | ||
412 | /* | |
413 | * Stage 8 of the trace events. | |
414 | * | |
415 | * Register/unregister probes at module load/unload. | |
416 | */ | |
417 | ||
418 | /* Reset all macros within TRACE_EVENT */ | |
419 | #include "lttng-tracepoint-events-reset.h" | |
420 | ||
421 | #define TP_ID1(_token, _system) _token##_system | |
422 | #define TP_ID(_token, _system) TP_ID1(_token, _system) | |
423 | ||
424 | static void __attribute__((constructor)) | |
425 | TP_ID(__lttng_events_init__, TRACE_SYSTEM)(void) | |
426 | { | |
427 | int ret; | |
428 | ||
429 | ret = ltt_probe_register(&TP_ID(__probe_desc___, TRACE_SYSTEM)); | |
430 | assert(!ret); | |
431 | } | |
432 | ||
433 | static void __attribute__((destructor)) | |
434 | TP_ID(__lttng_events_exit__, TRACE_SYSTEM)(void) | |
435 | { | |
436 | ltt_probe_unregister(&TP_ID(__probe_desc___, TRACE_SYSTEM)); | |
437 | } | |
438 | ||
439 | #undef TP_ID1 | |
440 | #undef TP_ID |