Fix: ctf_user_integer should not trigger page fault
[lttng-modules.git] / probes / lttng-events.h
1 /*
2 * lttng-events.h
3 *
4 * Copyright (C) 2009 Steven Rostedt <rostedt@goodmis.org>
5 * Copyright (C) 2009-2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; only
10 * version 2.1 of the License.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <linux/uaccess.h>
23 #include <linux/debugfs.h>
24 #include <linux/rculist.h>
25 #include "lttng.h"
26 #include "lttng-types.h"
27 #include "lttng-probe-user.h"
28 #include "../wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
29 #include "../wrapper/ringbuffer/frontend_types.h"
30 #include "../wrapper/ringbuffer/backend.h"
31 #include "../wrapper/rcu.h"
32 #include "../lttng-events.h"
33 #include "../lttng-tracer-core.h"
34
35 /*
36 * Macro declarations used for all stages.
37 */
38
39 /*
40 * LTTng name mapping macros. LTTng remaps some of the kernel events to
41 * enforce name-spacing.
42 */
43 #undef LTTNG_TRACEPOINT_EVENT_MAP
44 #define LTTNG_TRACEPOINT_EVENT_MAP(name, map, proto, args, fields) \
45 LTTNG_TRACEPOINT_EVENT_CLASS(map, \
46 PARAMS(proto), \
47 PARAMS(args), \
48 PARAMS(fields)) \
49 LTTNG_TRACEPOINT_EVENT_INSTANCE_MAP(map, name, map, PARAMS(proto), PARAMS(args))
50
51 #undef LTTNG_TRACEPOINT_EVENT_MAP_NOARGS
52 #define LTTNG_TRACEPOINT_EVENT_MAP_NOARGS(name, map, fields) \
53 LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS(map, \
54 PARAMS(fields)) \
55 LTTNG_TRACEPOINT_EVENT_INSTANCE_MAP_NOARGS(map, name, map)
56
57 #undef LTTNG_TRACEPOINT_EVENT_CODE_MAP
58 #define LTTNG_TRACEPOINT_EVENT_CODE_MAP(name, map, proto, args, _locvar, _code, fields) \
59 LTTNG_TRACEPOINT_EVENT_CLASS_CODE(map, \
60 PARAMS(proto), \
61 PARAMS(args), \
62 PARAMS(_locvar), \
63 PARAMS(_code), \
64 PARAMS(fields)) \
65 LTTNG_TRACEPOINT_EVENT_INSTANCE_MAP(map, name, map, PARAMS(proto), PARAMS(args))
66
67 #undef LTTNG_TRACEPOINT_EVENT_CODE
68 #define LTTNG_TRACEPOINT_EVENT_CODE(name, proto, args, _locvar, _code, fields) \
69 LTTNG_TRACEPOINT_EVENT_CODE_MAP(name, name, \
70 PARAMS(proto), \
71 PARAMS(args), \
72 PARAMS(_locvar), \
73 PARAMS(_code), \
74 PARAMS(fields))
75
76 /*
77 * LTTNG_TRACEPOINT_EVENT_CLASS can be used to add a generic function
78 * handlers for events. That is, if all events have the same parameters
79 * and just have distinct trace points. Each tracepoint can be defined
80 * with LTTNG_TRACEPOINT_EVENT_INSTANCE and that will map the
81 * LTTNG_TRACEPOINT_EVENT_CLASS to the tracepoint.
82 *
83 * LTTNG_TRACEPOINT_EVENT is a one to one mapping between tracepoint and
84 * template.
85 */
86
87 #undef LTTNG_TRACEPOINT_EVENT
88 #define LTTNG_TRACEPOINT_EVENT(name, proto, args, fields) \
89 LTTNG_TRACEPOINT_EVENT_MAP(name, name, \
90 PARAMS(proto), \
91 PARAMS(args), \
92 PARAMS(fields))
93
94 #undef LTTNG_TRACEPOINT_EVENT_NOARGS
95 #define LTTNG_TRACEPOINT_EVENT_NOARGS(name, fields) \
96 LTTNG_TRACEPOINT_EVENT_MAP_NOARGS(name, name, PARAMS(fields))
97
98 #undef LTTNG_TRACEPOINT_EVENT_INSTANCE
99 #define LTTNG_TRACEPOINT_EVENT_INSTANCE(template, name, proto, args) \
100 LTTNG_TRACEPOINT_EVENT_INSTANCE_MAP(template, name, name, PARAMS(proto), PARAMS(args))
101
102 #undef LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS
103 #define LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS(template, name) \
104 LTTNG_TRACEPOINT_EVENT_INSTANCE_MAP_NOARGS(template, name, name)
105
106 #undef LTTNG_TRACEPOINT_EVENT_CLASS
107 #define LTTNG_TRACEPOINT_EVENT_CLASS(_name, _proto, _args, _fields) \
108 LTTNG_TRACEPOINT_EVENT_CLASS_CODE(_name, PARAMS(_proto), PARAMS(_args), , , \
109 PARAMS(_fields))
110
111 #undef LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS
112 #define LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS(_name, _fields) \
113 LTTNG_TRACEPOINT_EVENT_CLASS_CODE_NOARGS(_name, , , PARAMS(_fields))
114
115
116 /*
117 * Stage 1 of the trace events.
118 *
119 * Create dummy trace calls for each events, verifying that the LTTng module
120 * instrumentation headers match the kernel arguments. Will be optimized
121 * out by the compiler.
122 */
123
124 /* Reset all macros within TRACEPOINT_EVENT */
125 #include "lttng-events-reset.h"
126
127 #undef TP_PROTO
128 #define TP_PROTO(...) __VA_ARGS__
129
130 #undef TP_ARGS
131 #define TP_ARGS(...) __VA_ARGS__
132
133 #undef LTTNG_TRACEPOINT_EVENT_INSTANCE_MAP
134 #define LTTNG_TRACEPOINT_EVENT_INSTANCE_MAP(_template, _name, _map, _proto, _args) \
135 void trace_##_name(_proto);
136
137 #undef LTTNG_TRACEPOINT_EVENT_INSTANCE_MAP_NOARGS
138 #define LTTNG_TRACEPOINT_EVENT_INSTANCE_MAP_NOARGS(_template, _name, _map) \
139 void trace_##_name(void);
140
141 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
142
143 /*
144 * Stage 1.1 of the trace events.
145 *
146 * Create dummy trace prototypes for each event class, and for each used
147 * template. This will allow checking whether the prototypes from the
148 * class and the instance using the class actually match.
149 */
150
151 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
152
153 #undef TP_PROTO
154 #define TP_PROTO(...) __VA_ARGS__
155
156 #undef TP_ARGS
157 #define TP_ARGS(...) __VA_ARGS__
158
159 #undef LTTNG_TRACEPOINT_EVENT_INSTANCE_MAP
160 #define LTTNG_TRACEPOINT_EVENT_INSTANCE_MAP(_template, _name, _map, _proto, _args) \
161 void __event_template_proto___##_template(_proto);
162
163 #undef LTTNG_TRACEPOINT_EVENT_INSTANCE_MAP_NOARGS
164 #define LTTNG_TRACEPOINT_EVENT_INSTANCE_MAP_NOARGS(_template, _name, _map) \
165 void __event_template_proto___##_template(void);
166
167 #undef LTTNG_TRACEPOINT_EVENT_CLASS_CODE
168 #define LTTNG_TRACEPOINT_EVENT_CLASS_CODE(_name, _proto, _args, _locvar, _code, _fields) \
169 void __event_template_proto___##_name(_proto);
170
171 #undef LTTNG_TRACEPOINT_EVENT_CLASS_CODE_NOARGS
172 #define LTTNG_TRACEPOINT_EVENT_CLASS_CODE_NOARGS(_name, _locvar, _code, _fields) \
173 void __event_template_proto___##_name(void);
174
175 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
176
177 /*
178 * Stage 2 of the trace events.
179 *
180 * Create event field type metadata section.
181 * Each event produce an array of fields.
182 */
183
184 /* Reset all macros within TRACEPOINT_EVENT */
185 #include "lttng-events-reset.h"
186 #include "lttng-events-write.h"
187 #include "lttng-events-nowrite.h"
188
189 #undef _ctf_integer_ext
190 #define _ctf_integer_ext(_type, _item, _src, _byte_order, _base, _user, _nowrite) \
191 { \
192 .name = #_item, \
193 .type = __type_integer(_type, _byte_order, _base, none),\
194 .nowrite = _nowrite, \
195 .user = _user, \
196 },
197
198 #undef _ctf_array_encoded
199 #define _ctf_array_encoded(_type, _item, _src, _length, _encoding, _user, _nowrite) \
200 { \
201 .name = #_item, \
202 .type = \
203 { \
204 .atype = atype_array, \
205 .u = \
206 { \
207 .array = \
208 { \
209 .elem_type = __type_integer(_type, __BYTE_ORDER, 10, _encoding), \
210 .length = _length, \
211 } \
212 } \
213 }, \
214 .nowrite = _nowrite, \
215 .user = _user, \
216 },
217
218 #undef _ctf_sequence_encoded
219 #define _ctf_sequence_encoded(_type, _item, _src, \
220 _length_type, _src_length, _encoding, \
221 _byte_order, _base, _user, _nowrite) \
222 { \
223 .name = #_item, \
224 .type = \
225 { \
226 .atype = atype_sequence, \
227 .u = \
228 { \
229 .sequence = \
230 { \
231 .length_type = __type_integer(_length_type, __BYTE_ORDER, 10, none), \
232 .elem_type = __type_integer(_type, _byte_order, _base, _encoding), \
233 }, \
234 }, \
235 }, \
236 .nowrite = _nowrite, \
237 .user = _user, \
238 },
239
240 #undef _ctf_string
241 #define _ctf_string(_item, _src, _user, _nowrite) \
242 { \
243 .name = #_item, \
244 .type = \
245 { \
246 .atype = atype_string, \
247 .u = \
248 { \
249 .basic = { .string = { .encoding = lttng_encode_UTF8 } } \
250 }, \
251 }, \
252 .nowrite = _nowrite, \
253 .user = _user, \
254 },
255
256 #undef TP_FIELDS
257 #define TP_FIELDS(...) __VA_ARGS__ /* Only one used in this phase */
258
259 #undef LTTNG_TRACEPOINT_EVENT_CLASS_CODE_NOARGS
260 #define LTTNG_TRACEPOINT_EVENT_CLASS_CODE_NOARGS(_name, _locvar, _code, _fields) \
261 static const struct lttng_event_field __event_fields___##_name[] = { \
262 _fields \
263 };
264
265 #undef LTTNG_TRACEPOINT_EVENT_CLASS_CODE
266 #define LTTNG_TRACEPOINT_EVENT_CLASS_CODE(_name, _proto, _args, _locvar, _code, _fields) \
267 LTTNG_TRACEPOINT_EVENT_CLASS_CODE_NOARGS(_name, _locvar, _code, PARAMS(_fields))
268
269 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
270
271 /*
272 * Stage 3 of the trace events.
273 *
274 * Create probe callback prototypes.
275 */
276
277 /* Reset all macros within TRACEPOINT_EVENT */
278 #include "lttng-events-reset.h"
279
280 #undef TP_PROTO
281 #define TP_PROTO(...) __VA_ARGS__
282
283 #undef LTTNG_TRACEPOINT_EVENT_CLASS_CODE
284 #define LTTNG_TRACEPOINT_EVENT_CLASS_CODE(_name, _proto, _args, _locvar, _code, _fields) \
285 static void __event_probe__##_name(void *__data, _proto);
286
287 #undef LTTNG_TRACEPOINT_EVENT_CLASS_CODE_NOARGS
288 #define LTTNG_TRACEPOINT_EVENT_CLASS_CODE_NOARGS(_name, _locvar, _code, _fields) \
289 static void __event_probe__##_name(void *__data);
290
291 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
292
293 /*
294 * Stage 4 of the trace events.
295 *
296 * Create static inline function that calculates event size.
297 */
298
299 /* Reset all macros within TRACEPOINT_EVENT */
300 #include "lttng-events-reset.h"
301 #include "lttng-events-write.h"
302
303 #undef _ctf_integer_ext
304 #define _ctf_integer_ext(_type, _item, _src, _byte_order, _base, _user, _nowrite) \
305 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
306 __event_len += sizeof(_type);
307
308 #undef _ctf_array_encoded
309 #define _ctf_array_encoded(_type, _item, _src, _length, _encoding, _user, _nowrite) \
310 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
311 __event_len += sizeof(_type) * (_length);
312
313 #undef _ctf_sequence_encoded
314 #define _ctf_sequence_encoded(_type, _item, _src, _length_type, \
315 _src_length, _encoding, _byte_order, _base, _user, _nowrite) \
316 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_length_type)); \
317 __event_len += sizeof(_length_type); \
318 __event_len += lib_ring_buffer_align(__event_len, lttng_alignof(_type)); \
319 __dynamic_len[__dynamic_len_idx] = (_src_length); \
320 __event_len += sizeof(_type) * __dynamic_len[__dynamic_len_idx]; \
321 __dynamic_len_idx++;
322
323 /*
324 * ctf_user_string includes \0. If returns 0, it faulted, so we set size to
325 * 1 (\0 only).
326 */
327 #undef _ctf_string
328 #define _ctf_string(_item, _src, _user, _nowrite) \
329 if (_user) \
330 __event_len += __dynamic_len[__dynamic_len_idx++] = \
331 max_t(size_t, lttng_strlen_user_inatomic(_src), 1); \
332 else \
333 __event_len += __dynamic_len[__dynamic_len_idx++] = \
334 strlen(_src) + 1;
335
336 #undef TP_PROTO
337 #define TP_PROTO(...) __VA_ARGS__
338
339 #undef TP_FIELDS
340 #define TP_FIELDS(...) __VA_ARGS__
341
342 #undef TP_locvar
343 #define TP_locvar(...) __VA_ARGS__
344
345 #undef LTTNG_TRACEPOINT_EVENT_CLASS_CODE
346 #define LTTNG_TRACEPOINT_EVENT_CLASS_CODE(_name, _proto, _args, _locvar, _code, _fields) \
347 static inline size_t __event_get_size__##_name(size_t *__dynamic_len, \
348 void *__tp_locvar, _proto) \
349 { \
350 size_t __event_len = 0; \
351 unsigned int __dynamic_len_idx __attribute__((unused)) = 0; \
352 struct { _locvar } *tp_locvar __attribute__((unused)) = __tp_locvar; \
353 \
354 _fields \
355 return __event_len; \
356 }
357
358 #undef LTTNG_TRACEPOINT_EVENT_CLASS_CODE_NOARGS
359 #define LTTNG_TRACEPOINT_EVENT_CLASS_CODE_NOARGS(_name, _locvar, _code, _fields) \
360 static inline size_t __event_get_size__##_name(size_t *__dynamic_len, \
361 void *__tp_locvar) \
362 { \
363 size_t __event_len = 0; \
364 unsigned int __dynamic_len_idx __attribute__((unused)) = 0; \
365 struct { _locvar } *tp_locvar __attribute__((unused)) = __tp_locvar; \
366 \
367 _fields \
368 return __event_len; \
369 }
370
371 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
372
373
374 /*
375 * Stage 4.1 of tracepoint event generation.
376 *
377 * Create static inline function that layout the filter stack data.
378 * We make both write and nowrite data available to the filter.
379 */
380
381 /* Reset all macros within TRACEPOINT_EVENT */
382 #include "lttng-events-reset.h"
383 #include "lttng-events-write.h"
384 #include "lttng-events-nowrite.h"
385
386 #undef _ctf_integer_ext_fetched
387 #define _ctf_integer_ext_fetched(_type, _item, _src, _byte_order, _base, _nowrite) \
388 if (lttng_is_signed_type(_type)) { \
389 int64_t __ctf_tmp_int64; \
390 switch (sizeof(_type)) { \
391 case 1: \
392 { \
393 union { _type t; int8_t v; } __tmp = { (_type) (_src) }; \
394 __ctf_tmp_int64 = (int64_t) __tmp.v; \
395 break; \
396 } \
397 case 2: \
398 { \
399 union { _type t; int16_t v; } __tmp = { (_type) (_src) }; \
400 __ctf_tmp_int64 = (int64_t) __tmp.v; \
401 break; \
402 } \
403 case 4: \
404 { \
405 union { _type t; int32_t v; } __tmp = { (_type) (_src) }; \
406 __ctf_tmp_int64 = (int64_t) __tmp.v; \
407 break; \
408 } \
409 case 8: \
410 { \
411 union { _type t; int64_t v; } __tmp = { (_type) (_src) }; \
412 __ctf_tmp_int64 = (int64_t) __tmp.v; \
413 break; \
414 } \
415 default: \
416 BUG_ON(1); \
417 }; \
418 memcpy(__stack_data, &__ctf_tmp_int64, sizeof(int64_t)); \
419 } else { \
420 uint64_t __ctf_tmp_uint64; \
421 switch (sizeof(_type)) { \
422 case 1: \
423 { \
424 union { _type t; uint8_t v; } __tmp = { (_type) (_src) }; \
425 __ctf_tmp_uint64 = (uint64_t) __tmp.v; \
426 break; \
427 } \
428 case 2: \
429 { \
430 union { _type t; uint16_t v; } __tmp = { (_type) (_src) }; \
431 __ctf_tmp_uint64 = (uint64_t) __tmp.v; \
432 break; \
433 } \
434 case 4: \
435 { \
436 union { _type t; uint32_t v; } __tmp = { (_type) (_src) }; \
437 __ctf_tmp_uint64 = (uint64_t) __tmp.v; \
438 break; \
439 } \
440 case 8: \
441 { \
442 union { _type t; uint64_t v; } __tmp = { (_type) (_src) }; \
443 __ctf_tmp_uint64 = (uint64_t) __tmp.v; \
444 break; \
445 } \
446 default: \
447 BUG_ON(1); \
448 }; \
449 memcpy(__stack_data, &__ctf_tmp_uint64, sizeof(uint64_t)); \
450 } \
451 __stack_data += sizeof(int64_t);
452
453 #undef _ctf_integer_ext_isuser0
454 #define _ctf_integer_ext_isuser0(_type, _item, _src, _byte_order, _base, _nowrite) \
455 _ctf_integer_ext_fetched(_type, _item, _src, _byte_order, _base, _nowrite)
456
457 #undef _ctf_integer_ext_isuser1
458 #define _ctf_integer_ext_isuser1(_type, _item, _user_src, _byte_order, _base, _nowrite) \
459 { \
460 union { \
461 char __array[sizeof(_user_src)]; \
462 __typeof__(_user_src) __v; \
463 } __tmp_fetch; \
464 if (lib_ring_buffer_copy_from_user_check_nofault(__tmp_fetch.__array, \
465 &(_user_src), sizeof(_user_src))) \
466 memset(__tmp_fetch.__array, 0, sizeof(__tmp_fetch.__array)); \
467 _ctf_integer_ext_fetched(_type, _item, __tmp_fetch.__v, _byte_order, _base, _nowrite) \
468 }
469
470 #undef _ctf_integer_ext
471 #define _ctf_integer_ext(_type, _item, _user_src, _byte_order, _base, _user, _nowrite) \
472 _ctf_integer_ext_isuser##_user(_type, _item, _user_src, _byte_order, _base, _nowrite)
473
474 #undef _ctf_array_encoded
475 #define _ctf_array_encoded(_type, _item, _src, _length, _encoding, _user, _nowrite) \
476 { \
477 unsigned long __ctf_tmp_ulong = (unsigned long) (_length); \
478 const void *__ctf_tmp_ptr = (_src); \
479 memcpy(__stack_data, &__ctf_tmp_ulong, sizeof(unsigned long)); \
480 __stack_data += sizeof(unsigned long); \
481 memcpy(__stack_data, &__ctf_tmp_ptr, sizeof(void *)); \
482 __stack_data += sizeof(void *); \
483 }
484
485 #undef _ctf_sequence_encoded
486 #define _ctf_sequence_encoded(_type, _item, _src, _length_type, \
487 _src_length, _encoding, _byte_order, _base, _user, _nowrite) \
488 { \
489 unsigned long __ctf_tmp_ulong = (unsigned long) (_src_length); \
490 const void *__ctf_tmp_ptr = (_src); \
491 memcpy(__stack_data, &__ctf_tmp_ulong, sizeof(unsigned long)); \
492 __stack_data += sizeof(unsigned long); \
493 memcpy(__stack_data, &__ctf_tmp_ptr, sizeof(void *)); \
494 __stack_data += sizeof(void *); \
495 }
496
497 #undef _ctf_string
498 #define _ctf_string(_item, _src, _user, _nowrite) \
499 { \
500 const void *__ctf_tmp_ptr = (_src); \
501 memcpy(__stack_data, &__ctf_tmp_ptr, sizeof(void *)); \
502 __stack_data += sizeof(void *); \
503 }
504
505 #undef TP_PROTO
506 #define TP_PROTO(...) __VA_ARGS__
507
508 #undef TP_FIELDS
509 #define TP_FIELDS(...) __VA_ARGS__
510
511 #undef TP_locvar
512 #define TP_locvar(...) __VA_ARGS__
513
514 #undef LTTNG_TRACEPOINT_EVENT_CLASS_CODE_NOARGS
515 #define LTTNG_TRACEPOINT_EVENT_CLASS_CODE_NOARGS(_name, _locvar, _code, _fields) \
516 static inline \
517 void __event_prepare_filter_stack__##_name(char *__stack_data, \
518 void *__tp_locvar) \
519 { \
520 struct { _locvar } *tp_locvar __attribute__((unused)) = __tp_locvar; \
521 \
522 _fields \
523 }
524
525 #undef LTTNG_TRACEPOINT_EVENT_CLASS_CODE
526 #define LTTNG_TRACEPOINT_EVENT_CLASS_CODE(_name, _proto, _args, _locvar, _code, _fields) \
527 static inline \
528 void __event_prepare_filter_stack__##_name(char *__stack_data, \
529 void *__tp_locvar, _proto) \
530 { \
531 struct { _locvar } *tp_locvar __attribute__((unused)) = __tp_locvar; \
532 \
533 _fields \
534 }
535
536 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
537
538 /*
539 * Stage 5 of the trace events.
540 *
541 * Create static inline function that calculates event payload alignment.
542 */
543
544 /* Reset all macros within TRACEPOINT_EVENT */
545 #include "lttng-events-reset.h"
546 #include "lttng-events-write.h"
547
548 #undef _ctf_integer_ext
549 #define _ctf_integer_ext(_type, _item, _src, _byte_order, _base, _user, _nowrite) \
550 __event_align = max_t(size_t, __event_align, lttng_alignof(_type));
551
552 #undef _ctf_array_encoded
553 #define _ctf_array_encoded(_type, _item, _src, _length, _encoding, _user, _nowrite) \
554 __event_align = max_t(size_t, __event_align, lttng_alignof(_type));
555
556 #undef _ctf_sequence_encoded
557 #define _ctf_sequence_encoded(_type, _item, _src, _length_type, \
558 _src_length, _encoding, _byte_order, _base, _user, _nowrite) \
559 __event_align = max_t(size_t, __event_align, lttng_alignof(_length_type)); \
560 __event_align = max_t(size_t, __event_align, lttng_alignof(_type));
561
562 #undef _ctf_string
563 #define _ctf_string(_item, _src, _user, _nowrite)
564
565 #undef TP_PROTO
566 #define TP_PROTO(...) __VA_ARGS__
567
568 #undef TP_FIELDS
569 #define TP_FIELDS(...) __VA_ARGS__
570
571 #undef TP_locvar
572 #define TP_locvar(...) __VA_ARGS__
573
574 #undef LTTNG_TRACEPOINT_EVENT_CLASS_CODE
575 #define LTTNG_TRACEPOINT_EVENT_CLASS_CODE(_name, _proto, _args, _locvar, _code, _fields) \
576 static inline size_t __event_get_align__##_name(void *__tp_locvar, _proto) \
577 { \
578 size_t __event_align = 1; \
579 struct { _locvar } *tp_locvar __attribute__((unused)) = __tp_locvar; \
580 \
581 _fields \
582 return __event_align; \
583 }
584
585 #undef LTTNG_TRACEPOINT_EVENT_CLASS_CODE_NOARGS
586 #define LTTNG_TRACEPOINT_EVENT_CLASS_CODE_NOARGS(_name, _locvar, _code, _fields) \
587 static inline size_t __event_get_align__##_name(void *__tp_locvar) \
588 { \
589 size_t __event_align = 1; \
590 struct { _locvar } *tp_locvar __attribute__((unused)) = __tp_locvar; \
591 \
592 _fields \
593 return __event_align; \
594 }
595
596 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
597
598 /*
599 * Stage 6 of tracepoint event generation.
600 *
601 * Create the probe function. This function calls event size calculation
602 * and writes event data into the buffer.
603 */
604
605 /* Reset all macros within TRACEPOINT_EVENT */
606 #include "lttng-events-reset.h"
607 #include "lttng-events-write.h"
608
609 #undef _ctf_integer_ext_fetched
610 #define _ctf_integer_ext_fetched(_type, _item, _src, _byte_order, _base, _nowrite) \
611 { \
612 _type __tmp = _src; \
613 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(__tmp));\
614 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
615 }
616
617 #undef _ctf_integer_ext_isuser0
618 #define _ctf_integer_ext_isuser0(_type, _item, _src, _byte_order, _base, _nowrite) \
619 _ctf_integer_ext_fetched(_type, _item, _src, _byte_order, _base, _nowrite)
620
621 #undef _ctf_integer_ext_isuser1
622 #define _ctf_integer_ext_isuser1(_type, _item, _user_src, _byte_order, _base, _nowrite) \
623 { \
624 union { \
625 char __array[sizeof(_user_src)]; \
626 __typeof__(_user_src) __v; \
627 } __tmp_fetch; \
628 if (lib_ring_buffer_copy_from_user_check_nofault(__tmp_fetch.__array, \
629 &(_user_src), sizeof(_user_src))) \
630 memset(__tmp_fetch.__array, 0, sizeof(__tmp_fetch.__array)); \
631 _ctf_integer_ext_fetched(_type, _item, __tmp_fetch.__v, _byte_order, _base, _nowrite) \
632 }
633
634 #undef _ctf_integer_ext
635 #define _ctf_integer_ext(_type, _item, _user_src, _byte_order, _base, _user, _nowrite) \
636 _ctf_integer_ext_isuser##_user(_type, _item, _user_src, _byte_order, _base, _nowrite)
637
638 #undef _ctf_array_encoded
639 #define _ctf_array_encoded(_type, _item, _src, _length, _encoding, _user, _nowrite) \
640 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
641 if (_user) { \
642 __chan->ops->event_write_from_user(&__ctx, _src, sizeof(_type) * (_length)); \
643 } else { \
644 __chan->ops->event_write(&__ctx, _src, sizeof(_type) * (_length)); \
645 }
646
647 #undef _ctf_sequence_encoded
648 #define _ctf_sequence_encoded(_type, _item, _src, _length_type, \
649 _src_length, _encoding, _byte_order, _base, _user, _nowrite) \
650 { \
651 _length_type __tmpl = __stackvar.__dynamic_len[__dynamic_len_idx]; \
652 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_length_type));\
653 __chan->ops->event_write(&__ctx, &__tmpl, sizeof(_length_type));\
654 } \
655 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(_type)); \
656 if (_user) { \
657 __chan->ops->event_write_from_user(&__ctx, _src, \
658 sizeof(_type) * __get_dynamic_len(dest)); \
659 } else { \
660 __chan->ops->event_write(&__ctx, _src, \
661 sizeof(_type) * __get_dynamic_len(dest)); \
662 }
663
664 #undef _ctf_string
665 #define _ctf_string(_item, _src, _user, _nowrite) \
666 lib_ring_buffer_align_ctx(&__ctx, lttng_alignof(*(_src))); \
667 if (_user) { \
668 __chan->ops->event_strcpy_from_user(&__ctx, _src, \
669 __get_dynamic_len(dest)); \
670 } else { \
671 __chan->ops->event_strcpy(&__ctx, _src, \
672 __get_dynamic_len(dest)); \
673 }
674
675 /* Beware: this get len actually consumes the len value */
676 #undef __get_dynamic_len
677 #define __get_dynamic_len(field) __stackvar.__dynamic_len[__dynamic_len_idx++]
678
679 #undef TP_PROTO
680 #define TP_PROTO(...) __VA_ARGS__
681
682 #undef TP_ARGS
683 #define TP_ARGS(...) __VA_ARGS__
684
685 #undef TP_FIELDS
686 #define TP_FIELDS(...) __VA_ARGS__
687
688 #undef TP_locvar
689 #define TP_locvar(...) __VA_ARGS__
690
691 #undef TP_code
692 #define TP_code(...) __VA_ARGS__
693
694 /*
695 * For state dump, check that "session" argument (mandatory) matches the
696 * session this event belongs to. Ensures that we write state dump data only
697 * into the started session, not into all sessions.
698 */
699 #ifdef TP_SESSION_CHECK
700 #define _TP_SESSION_CHECK(session, csession) (session == csession)
701 #else /* TP_SESSION_CHECK */
702 #define _TP_SESSION_CHECK(session, csession) 1
703 #endif /* TP_SESSION_CHECK */
704
705 /*
706 * Using twice size for filter stack data to hold size and pointer for
707 * each field (worse case). For integers, max size required is 64-bit.
708 * Same for double-precision floats. Those fit within
709 * 2*sizeof(unsigned long) for all supported architectures.
710 * Perform UNION (||) of filter runtime list.
711 */
712 #undef LTTNG_TRACEPOINT_EVENT_CLASS_CODE
713 #define LTTNG_TRACEPOINT_EVENT_CLASS_CODE(_name, _proto, _args, _locvar, _code, _fields) \
714 static void __event_probe__##_name(void *__data, _proto) \
715 { \
716 struct probe_local_vars { _locvar }; \
717 struct lttng_event *__event = __data; \
718 struct lttng_channel *__chan = __event->chan; \
719 struct lttng_session *__session = __chan->session; \
720 struct lib_ring_buffer_ctx __ctx; \
721 size_t __event_len, __event_align; \
722 size_t __dynamic_len_idx __attribute__((unused)) = 0; \
723 union { \
724 size_t __dynamic_len[ARRAY_SIZE(__event_fields___##_name)]; \
725 char __filter_stack_data[2 * sizeof(unsigned long) * ARRAY_SIZE(__event_fields___##_name)]; \
726 } __stackvar; \
727 int __ret; \
728 struct probe_local_vars __tp_locvar; \
729 struct probe_local_vars *tp_locvar __attribute__((unused)) = \
730 &__tp_locvar; \
731 struct lttng_pid_tracker *__lpf; \
732 \
733 if (!_TP_SESSION_CHECK(session, __session)) \
734 return; \
735 if (unlikely(!ACCESS_ONCE(__session->active))) \
736 return; \
737 if (unlikely(!ACCESS_ONCE(__chan->enabled))) \
738 return; \
739 if (unlikely(!ACCESS_ONCE(__event->enabled))) \
740 return; \
741 __lpf = lttng_rcu_dereference(__session->pid_tracker); \
742 if (__lpf && likely(!lttng_pid_tracker_lookup(__lpf, current->pid))) \
743 return; \
744 _code \
745 if (unlikely(!list_empty(&__event->bytecode_runtime_head))) { \
746 struct lttng_bytecode_runtime *bc_runtime; \
747 int __filter_record = __event->has_enablers_without_bytecode; \
748 \
749 __event_prepare_filter_stack__##_name(__stackvar.__filter_stack_data, \
750 tp_locvar, _args); \
751 lttng_list_for_each_entry_rcu(bc_runtime, &__event->bytecode_runtime_head, node) { \
752 if (unlikely(bc_runtime->filter(bc_runtime, \
753 __stackvar.__filter_stack_data) & LTTNG_FILTER_RECORD_FLAG)) \
754 __filter_record = 1; \
755 } \
756 if (likely(!__filter_record)) \
757 return; \
758 } \
759 __event_len = __event_get_size__##_name(__stackvar.__dynamic_len, \
760 tp_locvar, _args); \
761 __event_align = __event_get_align__##_name(tp_locvar, _args); \
762 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
763 __event_align, -1); \
764 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
765 if (__ret < 0) \
766 return; \
767 _fields \
768 __chan->ops->event_commit(&__ctx); \
769 }
770
771 #undef LTTNG_TRACEPOINT_EVENT_CLASS_CODE_NOARGS
772 #define LTTNG_TRACEPOINT_EVENT_CLASS_CODE_NOARGS(_name, _locvar, _code, _fields) \
773 static void __event_probe__##_name(void *__data) \
774 { \
775 struct probe_local_vars { _locvar }; \
776 struct lttng_event *__event = __data; \
777 struct lttng_channel *__chan = __event->chan; \
778 struct lttng_session *__session = __chan->session; \
779 struct lib_ring_buffer_ctx __ctx; \
780 size_t __event_len, __event_align; \
781 size_t __dynamic_len_idx __attribute__((unused)) = 0; \
782 union { \
783 size_t __dynamic_len[ARRAY_SIZE(__event_fields___##_name)]; \
784 char __filter_stack_data[2 * sizeof(unsigned long) * ARRAY_SIZE(__event_fields___##_name)]; \
785 } __stackvar; \
786 int __ret; \
787 struct probe_local_vars __tp_locvar; \
788 struct probe_local_vars *tp_locvar __attribute__((unused)) = \
789 &__tp_locvar; \
790 struct lttng_pid_tracker *__lpf; \
791 \
792 if (!_TP_SESSION_CHECK(session, __session)) \
793 return; \
794 if (unlikely(!ACCESS_ONCE(__session->active))) \
795 return; \
796 if (unlikely(!ACCESS_ONCE(__chan->enabled))) \
797 return; \
798 if (unlikely(!ACCESS_ONCE(__event->enabled))) \
799 return; \
800 __lpf = lttng_rcu_dereference(__session->pid_tracker); \
801 if (__lpf && likely(!lttng_pid_tracker_lookup(__lpf, current->pid))) \
802 return; \
803 _code \
804 if (unlikely(!list_empty(&__event->bytecode_runtime_head))) { \
805 struct lttng_bytecode_runtime *bc_runtime; \
806 int __filter_record = __event->has_enablers_without_bytecode; \
807 \
808 __event_prepare_filter_stack__##_name(__stackvar.__filter_stack_data, \
809 tp_locvar); \
810 lttng_list_for_each_entry_rcu(bc_runtime, &__event->bytecode_runtime_head, node) { \
811 if (unlikely(bc_runtime->filter(bc_runtime, \
812 __stackvar.__filter_stack_data) & LTTNG_FILTER_RECORD_FLAG)) \
813 __filter_record = 1; \
814 } \
815 if (likely(!__filter_record)) \
816 return; \
817 } \
818 __event_len = __event_get_size__##_name(__stackvar.__dynamic_len, tp_locvar); \
819 __event_align = __event_get_align__##_name(tp_locvar); \
820 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
821 __event_align, -1); \
822 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
823 if (__ret < 0) \
824 return; \
825 _fields \
826 __chan->ops->event_commit(&__ctx); \
827 }
828
829 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
830
831 #undef __get_dynamic_len
832
833 /*
834 * Stage 7 of the trace events.
835 *
836 * Create event descriptions.
837 */
838
839 /* Named field types must be defined in lttng-types.h */
840
841 #include "lttng-events-reset.h" /* Reset all macros within LTTNG_TRACEPOINT_EVENT */
842
843 #ifndef TP_PROBE_CB
844 #define TP_PROBE_CB(_template) &__event_probe__##_template
845 #endif
846
847 #undef LTTNG_TRACEPOINT_EVENT_INSTANCE_MAP_NOARGS
848 #define LTTNG_TRACEPOINT_EVENT_INSTANCE_MAP_NOARGS(_template, _name, _map) \
849 static const struct lttng_event_desc __event_desc___##_map = { \
850 .fields = __event_fields___##_template, \
851 .name = #_map, \
852 .kname = #_name, \
853 .probe_callback = (void *) TP_PROBE_CB(_template), \
854 .nr_fields = ARRAY_SIZE(__event_fields___##_template), \
855 .owner = THIS_MODULE, \
856 };
857
858 #undef LTTNG_TRACEPOINT_EVENT_INSTANCE_MAP
859 #define LTTNG_TRACEPOINT_EVENT_INSTANCE_MAP(_template, _name, _map, _proto, _args) \
860 LTTNG_TRACEPOINT_EVENT_INSTANCE_MAP_NOARGS(_template, _name, _map)
861
862 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
863
864 /*
865 * Stage 8 of the trace events.
866 *
867 * Create an array of event description pointers.
868 */
869
870 #include "lttng-events-reset.h" /* Reset all macros within LTTNG_TRACEPOINT_EVENT */
871
872 #undef LTTNG_TRACEPOINT_EVENT_INSTANCE_MAP_NOARGS
873 #define LTTNG_TRACEPOINT_EVENT_INSTANCE_MAP_NOARGS(_template, _name, _map) \
874 &__event_desc___##_map,
875
876 #undef LTTNG_TRACEPOINT_EVENT_INSTANCE_MAP
877 #define LTTNG_TRACEPOINT_EVENT_INSTANCE_MAP(_template, _name, _map, _proto, _args) \
878 LTTNG_TRACEPOINT_EVENT_INSTANCE_MAP_NOARGS(_template, _name, _map)
879
880 #define TP_ID1(_token, _system) _token##_system
881 #define TP_ID(_token, _system) TP_ID1(_token, _system)
882
883 static const struct lttng_event_desc *TP_ID(__event_desc___, TRACE_SYSTEM)[] = {
884 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
885 };
886
887 #undef TP_ID1
888 #undef TP_ID
889
890 /*
891 * Stage 9 of the trace events.
892 *
893 * Create a toplevel descriptor for the whole probe.
894 */
895
896 #define TP_ID1(_token, _system) _token##_system
897 #define TP_ID(_token, _system) TP_ID1(_token, _system)
898
899 /* non-const because list head will be modified when registered. */
900 static __used struct lttng_probe_desc TP_ID(__probe_desc___, TRACE_SYSTEM) = {
901 .provider = __stringify(TRACE_SYSTEM),
902 .event_desc = TP_ID(__event_desc___, TRACE_SYSTEM),
903 .nr_events = ARRAY_SIZE(TP_ID(__event_desc___, TRACE_SYSTEM)),
904 .head = { NULL, NULL },
905 .lazy_init_head = { NULL, NULL },
906 .lazy = 0,
907 };
908
909 #undef TP_ID1
910 #undef TP_ID
911
912 /*
913 * Stage 10 of the trace events.
914 *
915 * Register/unregister probes at module load/unload.
916 */
917
918 #include "lttng-events-reset.h" /* Reset all macros within LTTNG_TRACEPOINT_EVENT */
919
920 #define TP_ID1(_token, _system) _token##_system
921 #define TP_ID(_token, _system) TP_ID1(_token, _system)
922 #define module_init_eval1(_token, _system) module_init(_token##_system)
923 #define module_init_eval(_token, _system) module_init_eval1(_token, _system)
924 #define module_exit_eval1(_token, _system) module_exit(_token##_system)
925 #define module_exit_eval(_token, _system) module_exit_eval1(_token, _system)
926
927 #ifndef TP_MODULE_NOINIT
928 static int TP_ID(__lttng_events_init__, TRACE_SYSTEM)(void)
929 {
930 wrapper_vmalloc_sync_all();
931 return lttng_probe_register(&TP_ID(__probe_desc___, TRACE_SYSTEM));
932 }
933
934 static void TP_ID(__lttng_events_exit__, TRACE_SYSTEM)(void)
935 {
936 lttng_probe_unregister(&TP_ID(__probe_desc___, TRACE_SYSTEM));
937 }
938
939 #ifndef TP_MODULE_NOAUTOLOAD
940 module_init_eval(__lttng_events_init__, TRACE_SYSTEM);
941 module_exit_eval(__lttng_events_exit__, TRACE_SYSTEM);
942 #endif
943
944 #endif
945
946 #undef module_init_eval
947 #undef module_exit_eval
948 #undef TP_ID1
949 #undef TP_ID
950
951 #undef TP_PROTO
952 #undef TP_ARGS
This page took 0.048297 seconds and 5 git commands to generate.