1 /* SPDX-License-Identifier: GPL-2.0-only */
2 #ifndef _LINUX_TRACEPOINT_H
3 #define _LINUX_TRACEPOINT_H
4
5 /*
6 * Kernel Tracepoint API.
7 *
8 * See Documentation/trace/tracepoints.rst.
9 *
10 * Copyright (C) 2008-2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
11 *
12 * Heavily inspired from the Linux Kernel Markers.
13 */
14
15 #include <linux/smp.h>
16 #include <linux/srcu.h>
17 #include <linux/errno.h>
18 #include <linux/types.h>
19 #include <linux/cpumask.h>
20 #include <linux/rcupdate.h>
21 #include <linux/tracepoint-defs.h>
22
23 struct module;
24 struct tracepoint;
25 struct notifier_block;
26
27 struct trace_eval_map {
28 const char *system;
29 const char *eval_string;
30 unsigned long eval_value;
31 };
32
33 #define TRACEPOINT_DEFAULT_PRIO 10
34
35 extern struct srcu_struct tracepoint_srcu;
36
37 extern int
38 tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data);
39 extern int
40 tracepoint_probe_register_prio(struct tracepoint *tp, void *probe, void *data,
41 int prio);
42 extern int
43 tracepoint_probe_register_prio_may_exist(struct tracepoint *tp, void *probe, void *data,
44 int prio);
45 extern int
46 tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data);
47 static inline int
tracepoint_probe_register_may_exist(struct tracepoint * tp,void * probe,void * data)48 tracepoint_probe_register_may_exist(struct tracepoint *tp, void *probe,
49 void *data)
50 {
51 return tracepoint_probe_register_prio_may_exist(tp, probe, data,
52 TRACEPOINT_DEFAULT_PRIO);
53 }
54 extern void
55 for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv),
56 void *priv);
57
58 #ifdef CONFIG_MODULES
59 struct tp_module {
60 struct list_head list;
61 struct module *mod;
62 };
63
64 bool trace_module_has_bad_taint(struct module *mod);
65 extern int register_tracepoint_module_notifier(struct notifier_block *nb);
66 extern int unregister_tracepoint_module_notifier(struct notifier_block *nb);
67 #else
trace_module_has_bad_taint(struct module * mod)68 static inline bool trace_module_has_bad_taint(struct module *mod)
69 {
70 return false;
71 }
72 static inline
register_tracepoint_module_notifier(struct notifier_block * nb)73 int register_tracepoint_module_notifier(struct notifier_block *nb)
74 {
75 return 0;
76 }
77 static inline
unregister_tracepoint_module_notifier(struct notifier_block * nb)78 int unregister_tracepoint_module_notifier(struct notifier_block *nb)
79 {
80 return 0;
81 }
82 #endif /* CONFIG_MODULES */
83
84 /*
85 * tracepoint_synchronize_unregister must be called between the last tracepoint
86 * probe unregistration and the end of module exit to make sure there is no
87 * caller executing a probe when it is freed.
88 */
89 #ifdef CONFIG_TRACEPOINTS
tracepoint_synchronize_unregister(void)90 static inline void tracepoint_synchronize_unregister(void)
91 {
92 synchronize_srcu(&tracepoint_srcu);
93 synchronize_rcu();
94 }
95 #else
tracepoint_synchronize_unregister(void)96 static inline void tracepoint_synchronize_unregister(void)
97 { }
98 #endif
99
100 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
101 extern int syscall_regfunc(void);
102 extern void syscall_unregfunc(void);
103 #endif /* CONFIG_HAVE_SYSCALL_TRACEPOINTS */
104
105 #define PARAMS(args...) args
106
107 #define TRACE_DEFINE_ENUM(x)
108 #define TRACE_DEFINE_SIZEOF(x)
109
110 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
tracepoint_ptr_deref(tracepoint_ptr_t * p)111 static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p)
112 {
113 return offset_to_ptr(p);
114 }
115
116 #define __TRACEPOINT_ENTRY(name) \
117 asm(" .section \"__tracepoints_ptrs\", \"a\" \n" \
118 " .balign 4 \n" \
119 " .long __tracepoint_" #name " - . \n" \
120 " .previous \n")
121 #else
tracepoint_ptr_deref(tracepoint_ptr_t * p)122 static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p)
123 {
124 return *p;
125 }
126
127 #define __TRACEPOINT_ENTRY(name) \
128 static tracepoint_ptr_t __tracepoint_ptr_##name __used \
129 __attribute__((section("__tracepoints_ptrs"))) = \
130 &__tracepoint_##name
131 #endif
132
133 #endif /* _LINUX_TRACEPOINT_H */
134
135 /*
136 * Note: we keep the TRACE_EVENT and DECLARE_TRACE outside the include
137 * file ifdef protection.
138 * This is due to the way trace events work. If a file includes two
139 * trace event headers under one "CREATE_TRACE_POINTS" the first include
140 * will override the TRACE_EVENT and break the second include.
141 */
142
143 #ifndef DECLARE_TRACE
144
145 #define TP_PROTO(args...) args
146 #define TP_ARGS(args...) args
147 #define TP_CONDITION(args...) args
148
149 /*
150 * Individual subsystem my have a separate configuration to
151 * enable their tracepoints. By default, this file will create
152 * the tracepoints if CONFIG_TRACEPOINT is defined. If a subsystem
153 * wants to be able to disable its tracepoints from being created
154 * it can define NOTRACE before including the tracepoint headers.
155 */
156 #if defined(CONFIG_TRACEPOINTS) && !defined(NOTRACE)
157 #define TRACEPOINTS_ENABLED
158 #endif
159
160 #ifdef TRACEPOINTS_ENABLED
161
162 /*
163 * it_func[0] is never NULL because there is at least one element in the array
164 * when the array itself is non NULL.
165 *
166 * Note, the proto and args passed in includes "__data" as the first parameter.
167 * The reason for this is to handle the "void" prototype. If a tracepoint
168 * has a "void" prototype, then it is invalid to declare a function
169 * as "(void *, void)". The DECLARE_TRACE_NOARGS() will pass in just
170 * "void *data", where as the DECLARE_TRACE() will pass in "void *data, proto".
171 */
172 #define __DO_TRACE(tp, proto, args, cond, rcuidle) \
173 do { \
174 struct tracepoint_func *it_func_ptr; \
175 void *it_func; \
176 void *__data; \
177 int __maybe_unused __idx = 0; \
178 \
179 if (!(cond)) \
180 return; \
181 \
182 /* srcu can't be used from NMI */ \
183 WARN_ON_ONCE(rcuidle && in_nmi()); \
184 \
185 /* keep srcu and sched-rcu usage consistent */ \
186 preempt_disable_notrace(); \
187 \
188 /* \
189 * For rcuidle callers, use srcu since sched-rcu \
190 * doesn't work from the idle path. \
191 */ \
192 if (rcuidle) { \
193 __idx = srcu_read_lock_notrace(&tracepoint_srcu);\
194 rcu_irq_enter_irqson(); \
195 } \
196 \
197 it_func_ptr = rcu_dereference_raw((tp)->funcs); \
198 \
199 if (it_func_ptr) { \
200 do { \
201 it_func = (it_func_ptr)->func; \
202 __data = (it_func_ptr)->data; \
203 ((void(*)(proto))(it_func))(args); \
204 } while ((++it_func_ptr)->func); \
205 } \
206 \
207 if (rcuidle) { \
208 rcu_irq_exit_irqson(); \
209 srcu_read_unlock_notrace(&tracepoint_srcu, __idx);\
210 } \
211 \
212 preempt_enable_notrace(); \
213 } while (0)
214
215 #ifndef MODULE
216 #define __DECLARE_TRACE_RCU(name, proto, args, cond, data_proto, data_args) \
217 static inline void trace_##name##_rcuidle(proto) \
218 { \
219 if (static_key_false(&__tracepoint_##name.key)) \
220 __DO_TRACE(&__tracepoint_##name, \
221 TP_PROTO(data_proto), \
222 TP_ARGS(data_args), \
223 TP_CONDITION(cond), 1); \
224 }
225 #else
226 #define __DECLARE_TRACE_RCU(name, proto, args, cond, data_proto, data_args)
227 #endif
228
229 /*
230 * Make sure the alignment of the structure in the __tracepoints section will
231 * not add unwanted padding between the beginning of the section and the
232 * structure. Force alignment to the same alignment as the section start.
233 *
234 * When lockdep is enabled, we make sure to always test if RCU is
235 * "watching" regardless if the tracepoint is enabled or not. Tracepoints
236 * require RCU to be active, and it should always warn at the tracepoint
237 * site if it is not watching, as it will need to be active when the
238 * tracepoint is enabled.
239 */
240 #define __DECLARE_TRACE(name, proto, args, cond, data_proto, data_args) \
241 extern struct tracepoint __tracepoint_##name; \
242 static inline void trace_##name(proto) \
243 { \
244 if (static_key_false(&__tracepoint_##name.key)) \
245 __DO_TRACE(&__tracepoint_##name, \
246 TP_PROTO(data_proto), \
247 TP_ARGS(data_args), \
248 TP_CONDITION(cond), 0); \
249 if (IS_ENABLED(CONFIG_LOCKDEP) && (cond)) { \
250 WARN_ON_ONCE(!rcu_is_watching()); \
251 } \
252 } \
253 __DECLARE_TRACE_RCU(name, PARAMS(proto), PARAMS(args), \
254 PARAMS(cond), PARAMS(data_proto), PARAMS(data_args)) \
255 static inline int \
256 register_trace_##name(void (*probe)(data_proto), void *data) \
257 { \
258 return tracepoint_probe_register(&__tracepoint_##name, \
259 (void *)probe, data); \
260 } \
261 static inline int \
262 register_trace_prio_##name(void (*probe)(data_proto), void *data,\
263 int prio) \
264 { \
265 return tracepoint_probe_register_prio(&__tracepoint_##name, \
266 (void *)probe, data, prio); \
267 } \
268 static inline int \
269 unregister_trace_##name(void (*probe)(data_proto), void *data) \
270 { \
271 return tracepoint_probe_unregister(&__tracepoint_##name,\
272 (void *)probe, data); \
273 } \
274 static inline void \
275 check_trace_callback_type_##name(void (*cb)(data_proto)) \
276 { \
277 } \
278 static inline bool \
279 trace_##name##_enabled(void) \
280 { \
281 return static_key_false(&__tracepoint_##name.key); \
282 }
283
284 /*
285 * We have no guarantee that gcc and the linker won't up-align the tracepoint
286 * structures, so we create an array of pointers that will be used for iteration
287 * on the tracepoints.
288 */
289 #define DEFINE_TRACE_FN(name, reg, unreg) \
290 static const char __tpstrtab_##name[] \
291 __attribute__((section("__tracepoints_strings"))) = #name; \
292 struct tracepoint __tracepoint_##name \
293 __attribute__((section("__tracepoints"), used)) = \
294 { __tpstrtab_##name, STATIC_KEY_INIT_FALSE, reg, unreg, NULL };\
295 __TRACEPOINT_ENTRY(name);
296
297 #define DEFINE_TRACE(name) \
298 DEFINE_TRACE_FN(name, NULL, NULL);
299
300 #define EXPORT_TRACEPOINT_SYMBOL_GPL(name) \
301 EXPORT_SYMBOL_GPL(__tracepoint_##name)
302 #define EXPORT_TRACEPOINT_SYMBOL(name) \
303 EXPORT_SYMBOL(__tracepoint_##name)
304
305 #else /* !TRACEPOINTS_ENABLED */
306 #define __DECLARE_TRACE(name, proto, args, cond, data_proto, data_args) \
307 static inline void trace_##name(proto) \
308 { } \
309 static inline void trace_##name##_rcuidle(proto) \
310 { } \
311 static inline int \
312 register_trace_##name(void (*probe)(data_proto), \
313 void *data) \
314 { \
315 return -ENOSYS; \
316 } \
317 static inline int \
318 unregister_trace_##name(void (*probe)(data_proto), \
319 void *data) \
320 { \
321 return -ENOSYS; \
322 } \
323 static inline void check_trace_callback_type_##name(void (*cb)(data_proto)) \
324 { \
325 } \
326 static inline bool \
327 trace_##name##_enabled(void) \
328 { \
329 return false; \
330 }
331
332 #define DEFINE_TRACE_FN(name, reg, unreg)
333 #define DEFINE_TRACE(name)
334 #define EXPORT_TRACEPOINT_SYMBOL_GPL(name)
335 #define EXPORT_TRACEPOINT_SYMBOL(name)
336
337 #endif /* TRACEPOINTS_ENABLED */
338
339 #ifdef CONFIG_TRACING
340 /**
341 * tracepoint_string - register constant persistent string to trace system
342 * @str - a constant persistent string that will be referenced in tracepoints
343 *
344 * If constant strings are being used in tracepoints, it is faster and
345 * more efficient to just save the pointer to the string and reference
346 * that with a printf "%s" instead of saving the string in the ring buffer
347 * and wasting space and time.
348 *
349 * The problem with the above approach is that userspace tools that read
350 * the binary output of the trace buffers do not have access to the string.
351 * Instead they just show the address of the string which is not very
352 * useful to users.
353 *
354 * With tracepoint_string(), the string will be registered to the tracing
355 * system and exported to userspace via the debugfs/tracing/printk_formats
356 * file that maps the string address to the string text. This way userspace
357 * tools that read the binary buffers have a way to map the pointers to
358 * the ASCII strings they represent.
359 *
360 * The @str used must be a constant string and persistent as it would not
361 * make sense to show a string that no longer exists. But it is still fine
362 * to be used with modules, because when modules are unloaded, if they
363 * had tracepoints, the ring buffers are cleared too. As long as the string
364 * does not change during the life of the module, it is fine to use
365 * tracepoint_string() within a module.
366 */
367 #define tracepoint_string(str) \
368 ({ \
369 static const char *___tp_str __tracepoint_string = str; \
370 ___tp_str; \
371 })
372 #define __tracepoint_string __attribute__((section("__tracepoint_str"), used))
373 #else
374 /*
375 * tracepoint_string() is used to save the string address for userspace
376 * tracing tools. When tracing isn't configured, there's no need to save
377 * anything.
378 */
379 # define tracepoint_string(str) str
380 # define __tracepoint_string
381 #endif
382
383 /*
384 * The need for the DECLARE_TRACE_NOARGS() is to handle the prototype
385 * (void). "void" is a special value in a function prototype and can
386 * not be combined with other arguments. Since the DECLARE_TRACE()
387 * macro adds a data element at the beginning of the prototype,
388 * we need a way to differentiate "(void *data, proto)" from
389 * "(void *data, void)". The second prototype is invalid.
390 *
391 * DECLARE_TRACE_NOARGS() passes "void" as the tracepoint prototype
392 * and "void *__data" as the callback prototype.
393 *
394 * DECLARE_TRACE() passes "proto" as the tracepoint protoype and
395 * "void *__data, proto" as the callback prototype.
396 */
397 #define DECLARE_TRACE_NOARGS(name) \
398 __DECLARE_TRACE(name, void, , \
399 cpu_online(raw_smp_processor_id()), \
400 void *__data, __data)
401
402 #define DECLARE_TRACE(name, proto, args) \
403 __DECLARE_TRACE(name, PARAMS(proto), PARAMS(args), \
404 cpu_online(raw_smp_processor_id()), \
405 PARAMS(void *__data, proto), \
406 PARAMS(__data, args))
407
408 #define DECLARE_TRACE_CONDITION(name, proto, args, cond) \
409 __DECLARE_TRACE(name, PARAMS(proto), PARAMS(args), \
410 cpu_online(raw_smp_processor_id()) && (PARAMS(cond)), \
411 PARAMS(void *__data, proto), \
412 PARAMS(__data, args))
413
414 #define TRACE_EVENT_FLAGS(event, flag)
415
416 #define TRACE_EVENT_PERF_PERM(event, expr...)
417
418 #endif /* DECLARE_TRACE */
419
420 #ifndef TRACE_EVENT
421 /*
422 * For use with the TRACE_EVENT macro:
423 *
424 * We define a tracepoint, its arguments, its printk format
425 * and its 'fast binary record' layout.
426 *
427 * Firstly, name your tracepoint via TRACE_EVENT(name : the
428 * 'subsystem_event' notation is fine.
429 *
430 * Think about this whole construct as the
431 * 'trace_sched_switch() function' from now on.
432 *
433 *
434 * TRACE_EVENT(sched_switch,
435 *
436 * *
437 * * A function has a regular function arguments
438 * * prototype, declare it via TP_PROTO():
439 * *
440 *
441 * TP_PROTO(struct rq *rq, struct task_struct *prev,
442 * struct task_struct *next),
443 *
444 * *
445 * * Define the call signature of the 'function'.
446 * * (Design sidenote: we use this instead of a
447 * * TP_PROTO1/TP_PROTO2/TP_PROTO3 ugliness.)
448 * *
449 *
450 * TP_ARGS(rq, prev, next),
451 *
452 * *
453 * * Fast binary tracing: define the trace record via
454 * * TP_STRUCT__entry(). You can think about it like a
455 * * regular C structure local variable definition.
456 * *
457 * * This is how the trace record is structured and will
458 * * be saved into the ring buffer. These are the fields
459 * * that will be exposed to user-space in
460 * * /sys/kernel/debug/tracing/events/<*>/format.
461 * *
462 * * The declared 'local variable' is called '__entry'
463 * *
464 * * __field(pid_t, prev_prid) is equivalent to a standard declariton:
465 * *
466 * * pid_t prev_pid;
467 * *
468 * * __array(char, prev_comm, TASK_COMM_LEN) is equivalent to:
469 * *
470 * * char prev_comm[TASK_COMM_LEN];
471 * *
472 *
473 * TP_STRUCT__entry(
474 * __array( char, prev_comm, TASK_COMM_LEN )
475 * __field( pid_t, prev_pid )
476 * __field( int, prev_prio )
477 * __array( char, next_comm, TASK_COMM_LEN )
478 * __field( pid_t, next_pid )
479 * __field( int, next_prio )
480 * ),
481 *
482 * *
483 * * Assign the entry into the trace record, by embedding
484 * * a full C statement block into TP_fast_assign(). You
485 * * can refer to the trace record as '__entry' -
486 * * otherwise you can put arbitrary C code in here.
487 * *
488 * * Note: this C code will execute every time a trace event
489 * * happens, on an active tracepoint.
490 * *
491 *
492 * TP_fast_assign(
493 * memcpy(__entry->next_comm, next->comm, TASK_COMM_LEN);
494 * __entry->prev_pid = prev->pid;
495 * __entry->prev_prio = prev->prio;
496 * memcpy(__entry->prev_comm, prev->comm, TASK_COMM_LEN);
497 * __entry->next_pid = next->pid;
498 * __entry->next_prio = next->prio;
499 * ),
500 *
501 * *
502 * * Formatted output of a trace record via TP_printk().
503 * * This is how the tracepoint will appear under ftrace
504 * * plugins that make use of this tracepoint.
505 * *
506 * * (raw-binary tracing wont actually perform this step.)
507 * *
508 *
509 * TP_printk("task %s:%d [%d] ==> %s:%d [%d]",
510 * __entry->prev_comm, __entry->prev_pid, __entry->prev_prio,
511 * __entry->next_comm, __entry->next_pid, __entry->next_prio),
512 *
513 * );
514 *
515 * This macro construct is thus used for the regular printk format
516 * tracing setup, it is used to construct a function pointer based
517 * tracepoint callback (this is used by programmatic plugins and
518 * can also by used by generic instrumentation like SystemTap), and
519 * it is also used to expose a structured trace record in
520 * /sys/kernel/debug/tracing/events/.
521 *
522 * A set of (un)registration functions can be passed to the variant
523 * TRACE_EVENT_FN to perform any (un)registration work.
524 */
525
526 #define DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, print)
527 #define DEFINE_EVENT(template, name, proto, args) \
528 DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
529 #define DEFINE_EVENT_FN(template, name, proto, args, reg, unreg)\
530 DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
531 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
532 DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
533 #define DEFINE_EVENT_CONDITION(template, name, proto, \
534 args, cond) \
535 DECLARE_TRACE_CONDITION(name, PARAMS(proto), \
536 PARAMS(args), PARAMS(cond))
537
538 #define TRACE_EVENT(name, proto, args, struct, assign, print) \
539 DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
540 #define TRACE_EVENT_FN(name, proto, args, struct, \
541 assign, print, reg, unreg) \
542 DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
543 #define TRACE_EVENT_FN_COND(name, proto, args, cond, struct, \
544 assign, print, reg, unreg) \
545 DECLARE_TRACE_CONDITION(name, PARAMS(proto), \
546 PARAMS(args), PARAMS(cond))
547 #define TRACE_EVENT_CONDITION(name, proto, args, cond, \
548 struct, assign, print) \
549 DECLARE_TRACE_CONDITION(name, PARAMS(proto), \
550 PARAMS(args), PARAMS(cond))
551
552 #define TRACE_EVENT_FLAGS(event, flag)
553
554 #define TRACE_EVENT_PERF_PERM(event, expr...)
555
556 #define DECLARE_EVENT_NOP(name, proto, args) \
557 static inline void trace_##name(proto) \
558 { } \
559 static inline bool trace_##name##_enabled(void) \
560 { \
561 return false; \
562 }
563
564 #define TRACE_EVENT_NOP(name, proto, args, struct, assign, print) \
565 DECLARE_EVENT_NOP(name, PARAMS(proto), PARAMS(args))
566
567 #define DECLARE_EVENT_CLASS_NOP(name, proto, args, tstruct, assign, print)
568 #define DEFINE_EVENT_NOP(template, name, proto, args) \
569 DECLARE_EVENT_NOP(name, PARAMS(proto), PARAMS(args))
570
571 #endif /* ifdef TRACE_EVENT (see note above) */
572