1 /* SPDX-License-Identifier: GPL-2.0-only */
2 #ifndef _LINUX_TRACEPOINT_H
3 #define _LINUX_TRACEPOINT_H
6 * Kernel Tracepoint API.
8 * See Documentation/trace/tracepoints.rst.
10 * Copyright (C) 2008-2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
12 * Heavily inspired from the Linux Kernel Markers.
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>
25 struct notifier_block;
27 struct trace_eval_map {
29 const char *eval_string;
30 unsigned long eval_value;
33 #define TRACEPOINT_DEFAULT_PRIO 10
35 extern struct srcu_struct tracepoint_srcu;
38 tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data);
40 tracepoint_probe_register_prio(struct tracepoint *tp, void *probe, void *data,
43 tracepoint_probe_register_prio_may_exist(struct tracepoint *tp, void *probe, void *data,
46 tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data);
48 tracepoint_probe_register_may_exist(struct tracepoint *tp, void *probe,
51 return tracepoint_probe_register_prio_may_exist(tp, probe, data,
52 TRACEPOINT_DEFAULT_PRIO);
55 for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv),
60 struct list_head list;
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);
68 static inline bool trace_module_has_bad_taint(struct module *mod)
73 int register_tracepoint_module_notifier(struct notifier_block *nb)
78 int unregister_tracepoint_module_notifier(struct notifier_block *nb)
82 #endif /* CONFIG_MODULES */
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.
89 #ifdef CONFIG_TRACEPOINTS
90 static inline void tracepoint_synchronize_unregister(void)
92 synchronize_srcu(&tracepoint_srcu);
96 static inline void tracepoint_synchronize_unregister(void)
100 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
101 extern int syscall_regfunc(void);
102 extern void syscall_unregfunc(void);
103 #endif /* CONFIG_HAVE_SYSCALL_TRACEPOINTS */
105 #define PARAMS(args...) args
107 #define TRACE_DEFINE_ENUM(x)
108 #define TRACE_DEFINE_SIZEOF(x)
110 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
111 static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p)
113 return offset_to_ptr(p);
116 #define __TRACEPOINT_ENTRY(name) \
117 asm(" .section \"__tracepoints_ptrs\", \"a\" \n" \
119 " .long __tracepoint_" #name " - . \n" \
122 static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p)
127 #define __TRACEPOINT_ENTRY(name) \
128 static tracepoint_ptr_t __tracepoint_ptr_##name __used \
129 __attribute__((section("__tracepoints_ptrs"))) = \
133 #endif /* _LINUX_TRACEPOINT_H */
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.
143 #ifndef DECLARE_TRACE
145 #define TP_PROTO(args...) args
146 #define TP_ARGS(args...) args
147 #define TP_CONDITION(args...) args
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.
156 #if defined(CONFIG_TRACEPOINTS) && !defined(NOTRACE)
157 #define TRACEPOINTS_ENABLED
160 #ifdef TRACEPOINTS_ENABLED
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.
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".
172 #define __DO_TRACE(tp, proto, args, cond, rcuidle) \
174 struct tracepoint_func *it_func_ptr; \
177 int __maybe_unused __idx = 0; \
182 /* srcu can't be used from NMI */ \
183 WARN_ON_ONCE(rcuidle && in_nmi()); \
185 /* keep srcu and sched-rcu usage consistent */ \
186 preempt_disable_notrace(); \
189 * For rcuidle callers, use srcu since sched-rcu \
190 * doesn't work from the idle path. \
193 __idx = srcu_read_lock_notrace(&tracepoint_srcu);\
194 rcu_irq_enter_irqson(); \
197 it_func_ptr = rcu_dereference_raw((tp)->funcs); \
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); \
208 rcu_irq_exit_irqson(); \
209 srcu_read_unlock_notrace(&tracepoint_srcu, __idx);\
212 preempt_enable_notrace(); \
216 #define __DECLARE_TRACE_RCU(name, proto, args, cond, data_proto, data_args) \
217 static inline void trace_##name##_rcuidle(proto) \
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); \
226 #define __DECLARE_TRACE_RCU(name, proto, args, cond, data_proto, data_args)
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.
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.
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) \
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()); \
253 __DECLARE_TRACE_RCU(name, PARAMS(proto), PARAMS(args), \
254 PARAMS(cond), PARAMS(data_proto), PARAMS(data_args)) \
256 register_trace_##name(void (*probe)(data_proto), void *data) \
258 return tracepoint_probe_register(&__tracepoint_##name, \
259 (void *)probe, data); \
262 register_trace_prio_##name(void (*probe)(data_proto), void *data,\
265 return tracepoint_probe_register_prio(&__tracepoint_##name, \
266 (void *)probe, data, prio); \
269 unregister_trace_##name(void (*probe)(data_proto), void *data) \
271 return tracepoint_probe_unregister(&__tracepoint_##name,\
272 (void *)probe, data); \
275 check_trace_callback_type_##name(void (*cb)(data_proto)) \
279 trace_##name##_enabled(void) \
281 return static_key_false(&__tracepoint_##name.key); \
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.
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);
297 #define DEFINE_TRACE(name) \
298 DEFINE_TRACE_FN(name, NULL, NULL);
300 #define EXPORT_TRACEPOINT_SYMBOL_GPL(name) \
301 EXPORT_SYMBOL_GPL(__tracepoint_##name)
302 #define EXPORT_TRACEPOINT_SYMBOL(name) \
303 EXPORT_SYMBOL(__tracepoint_##name)
305 #else /* !TRACEPOINTS_ENABLED */
306 #define __DECLARE_TRACE(name, proto, args, cond, data_proto, data_args) \
307 static inline void trace_##name(proto) \
309 static inline void trace_##name##_rcuidle(proto) \
312 register_trace_##name(void (*probe)(data_proto), \
318 unregister_trace_##name(void (*probe)(data_proto), \
323 static inline void check_trace_callback_type_##name(void (*cb)(data_proto)) \
327 trace_##name##_enabled(void) \
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)
337 #endif /* TRACEPOINTS_ENABLED */
339 #ifdef CONFIG_TRACING
341 * tracepoint_string - register constant persistent string to trace system
342 * @str - a constant persistent string that will be referenced in tracepoints
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.
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
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.
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.
367 #define tracepoint_string(str) \
369 static const char *___tp_str __tracepoint_string = str; \
372 #define __tracepoint_string __attribute__((section("__tracepoint_str"), used))
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
379 # define tracepoint_string(str) str
380 # define __tracepoint_string
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.
391 * DECLARE_TRACE_NOARGS() passes "void" as the tracepoint prototype
392 * and "void *__data" as the callback prototype.
394 * DECLARE_TRACE() passes "proto" as the tracepoint protoype and
395 * "void *__data, proto" as the callback prototype.
397 #define DECLARE_TRACE_NOARGS(name) \
398 __DECLARE_TRACE(name, void, , \
399 cpu_online(raw_smp_processor_id()), \
400 void *__data, __data)
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))
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))
414 #define TRACE_EVENT_FLAGS(event, flag)
416 #define TRACE_EVENT_PERF_PERM(event, expr...)
418 #endif /* DECLARE_TRACE */
422 * For use with the TRACE_EVENT macro:
424 * We define a tracepoint, its arguments, its printk format
425 * and its 'fast binary record' layout.
427 * Firstly, name your tracepoint via TRACE_EVENT(name : the
428 * 'subsystem_event' notation is fine.
430 * Think about this whole construct as the
431 * 'trace_sched_switch() function' from now on.
434 * TRACE_EVENT(sched_switch,
437 * * A function has a regular function arguments
438 * * prototype, declare it via TP_PROTO():
441 * TP_PROTO(struct rq *rq, struct task_struct *prev,
442 * struct task_struct *next),
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.)
450 * TP_ARGS(rq, prev, next),
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.
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.
462 * * The declared 'local variable' is called '__entry'
464 * * __field(pid_t, prev_prid) is equivalent to a standard declariton:
468 * * __array(char, prev_comm, TASK_COMM_LEN) is equivalent to:
470 * * char prev_comm[TASK_COMM_LEN];
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 )
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.
488 * * Note: this C code will execute every time a trace event
489 * * happens, on an active tracepoint.
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;
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.
506 * * (raw-binary tracing wont actually perform this step.)
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),
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/.
522 * A set of (un)registration functions can be passed to the variant
523 * TRACE_EVENT_FN to perform any (un)registration work.
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, \
535 DECLARE_TRACE_CONDITION(name, PARAMS(proto), \
536 PARAMS(args), PARAMS(cond))
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))
552 #define TRACE_EVENT_FLAGS(event, flag)
554 #define TRACE_EVENT_PERF_PERM(event, expr...)
556 #define DECLARE_EVENT_NOP(name, proto, args) \
557 static inline void trace_##name(proto) \
559 static inline bool trace_##name##_enabled(void) \
564 #define TRACE_EVENT_NOP(name, proto, args, struct, assign, print) \
565 DECLARE_EVENT_NOP(name, PARAMS(proto), PARAMS(args))
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))
571 #endif /* ifdef TRACE_EVENT (see note above) */