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