xref: /linux/include/linux/tracepoint.h (revision 44ed32d16c9d0e0f3a4b594982a2bb168d2f56ea)
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/rcupdate.h>
20 #include <linux/rcupdate_trace.h>
21 #include <linux/tracepoint-defs.h>
22 #include <linux/static_call.h>
23 #include <linux/cfi.h>
24 
25 struct module;
26 struct tracepoint;
27 struct notifier_block;
28 
29 struct trace_eval_map {
30 	const char		*system;
31 	const char		*eval_string;
32 	unsigned long		eval_value;
33 };
34 
35 #define TRACEPOINT_DEFAULT_PRIO	10
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 void for_each_module_tracepoint(void (*fct)(struct tracepoint *,
68 					struct module *, void *),
69 				void *priv);
70 void for_each_tracepoint_in_module(struct module *,
71 				   void (*fct)(struct tracepoint *,
72 					struct module *, void *),
73 				   void *priv);
74 #else
trace_module_has_bad_taint(struct module * mod)75 static inline bool trace_module_has_bad_taint(struct module *mod)
76 {
77 	return false;
78 }
79 static inline
register_tracepoint_module_notifier(struct notifier_block * nb)80 int register_tracepoint_module_notifier(struct notifier_block *nb)
81 {
82 	return 0;
83 }
84 static inline
unregister_tracepoint_module_notifier(struct notifier_block * nb)85 int unregister_tracepoint_module_notifier(struct notifier_block *nb)
86 {
87 	return 0;
88 }
89 static inline
for_each_module_tracepoint(void (* fct)(struct tracepoint *,struct module *,void *),void * priv)90 void for_each_module_tracepoint(void (*fct)(struct tracepoint *,
91 					struct module *, void *),
92 				void *priv)
93 {
94 }
95 static inline
for_each_tracepoint_in_module(struct module * mod,void (* fct)(struct tracepoint *,struct module *,void *),void * priv)96 void for_each_tracepoint_in_module(struct module *mod,
97 				   void (*fct)(struct tracepoint *,
98 					struct module *, void *),
99 				   void *priv)
100 {
101 }
102 #endif /* CONFIG_MODULES */
103 
104 /*
105  * tracepoint_synchronize_unregister must be called between the last tracepoint
106  * probe unregistration and the end of module exit to make sure there is no
107  * caller executing a probe when it is freed.
108  *
109  * An alternative is to use the following for batch reclaim associated
110  * with a given tracepoint:
111  *
112  * - tracepoint_is_faultable() == false: call_srcu()
113  * - tracepoint_is_faultable() == true:  call_rcu_tasks_trace()
114  */
115 #ifdef CONFIG_TRACEPOINTS
116 extern struct srcu_struct tracepoint_srcu;
tracepoint_synchronize_unregister(void)117 static inline void tracepoint_synchronize_unregister(void)
118 {
119 	synchronize_rcu_tasks_trace();
120 	synchronize_srcu(&tracepoint_srcu);
121 }
tracepoint_is_faultable(struct tracepoint * tp)122 static inline bool tracepoint_is_faultable(struct tracepoint *tp)
123 {
124 	return tp->ext && tp->ext->faultable;
125 }
126 /*
127  * Run RCU callback with the appropriate grace period wait for non-faultable
128  * tracepoints, e.g., those used in atomic context.
129  */
call_tracepoint_unregister_atomic(struct rcu_head * rcu,rcu_callback_t func)130 static inline void call_tracepoint_unregister_atomic(struct rcu_head *rcu, rcu_callback_t func)
131 {
132 	call_srcu(&tracepoint_srcu, rcu, func);
133 }
134 /*
135  * Run RCU callback with the appropriate grace period wait for faultable
136  * tracepoints, e.g., those used in syscall context.
137  */
call_tracepoint_unregister_syscall(struct rcu_head * rcu,rcu_callback_t func)138 static inline void call_tracepoint_unregister_syscall(struct rcu_head *rcu, rcu_callback_t func)
139 {
140 	call_rcu_tasks_trace(rcu, func);
141 }
142 #else
tracepoint_synchronize_unregister(void)143 static inline void tracepoint_synchronize_unregister(void)
144 { }
tracepoint_is_faultable(struct tracepoint * tp)145 static inline bool tracepoint_is_faultable(struct tracepoint *tp)
146 {
147 	return false;
148 }
call_tracepoint_unregister_atomic(struct rcu_head * rcu,rcu_callback_t func)149 static inline void call_tracepoint_unregister_atomic(struct rcu_head *rcu, rcu_callback_t func)
150 {  }
call_tracepoint_unregister_syscall(struct rcu_head * rcu,rcu_callback_t func)151 static inline void call_tracepoint_unregister_syscall(struct rcu_head *rcu, rcu_callback_t func)
152 {  }
153 #endif
154 
155 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
156 extern int syscall_regfunc(void);
157 extern void syscall_unregfunc(void);
158 #endif /* CONFIG_HAVE_SYSCALL_TRACEPOINTS */
159 
160 #ifndef PARAMS
161 #define PARAMS(args...) args
162 #endif
163 
164 #define TRACE_DEFINE_ENUM(x)
165 #define TRACE_DEFINE_SIZEOF(x)
166 
167 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
tracepoint_ptr_deref(tracepoint_ptr_t * p)168 static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p)
169 {
170 	return offset_to_ptr(p);
171 }
172 
173 #define __TRACEPOINT_ENTRY(name)					\
174 	asm("	.section \"__tracepoints_ptrs\", \"a\"		\n"	\
175 	    "	.balign 4					\n"	\
176 	    "	.long 	__tracepoint_" #name " - .		\n"	\
177 	    "	.previous					\n")
178 #else
tracepoint_ptr_deref(tracepoint_ptr_t * p)179 static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p)
180 {
181 	return *p;
182 }
183 
184 #define __TRACEPOINT_ENTRY(name)					 \
185 	static tracepoint_ptr_t __tracepoint_ptr_##name __used		 \
186 	__section("__tracepoints_ptrs") = &__tracepoint_##name
187 #endif
188 
189 #endif /* _LINUX_TRACEPOINT_H */
190 
191 /*
192  * Note: we keep the TRACE_EVENT and DECLARE_TRACE outside the include
193  *  file ifdef protection.
194  *  This is due to the way trace events work. If a file includes two
195  *  trace event headers under one "CREATE_TRACE_POINTS" the first include
196  *  will override the TRACE_EVENT and break the second include.
197  */
198 
199 #ifndef DECLARE_TRACE
200 
201 #define TP_PROTO(args...)	args
202 #define TP_ARGS(args...)	args
203 #define TP_CONDITION(args...)	args
204 
205 /*
206  * Individual subsystem may have a separate configuration to
207  * enable their tracepoints. By default, this file will create
208  * the tracepoints if CONFIG_TRACEPOINTS is defined. If a subsystem
209  * wants to be able to disable its tracepoints from being created
210  * it can define NOTRACE before including the tracepoint headers.
211  */
212 #if defined(CONFIG_TRACEPOINTS) && !defined(NOTRACE)
213 #define TRACEPOINTS_ENABLED
214 #endif
215 
216 #ifdef TRACEPOINTS_ENABLED
217 
218 #ifdef CONFIG_HAVE_STATIC_CALL
219 #define __DO_TRACE_CALL(name, args)					\
220 	do {								\
221 		struct tracepoint_func *it_func_ptr;			\
222 		void *__data;						\
223 		it_func_ptr =						\
224 			rcu_dereference_raw((&__tracepoint_##name)->funcs); \
225 		if (it_func_ptr) {					\
226 			__data = (it_func_ptr)->data;			\
227 			static_call(tp_func_##name)(__data, args);	\
228 		}							\
229 	} while (0)
230 #else
231 #define __DO_TRACE_CALL(name, args)	__traceiter_##name(NULL, args)
232 #endif /* CONFIG_HAVE_STATIC_CALL */
233 
234 /*
235  * Declare an exported function that Rust code can call to trigger this
236  * tracepoint. This function does not include the static branch; that is done
237  * in Rust to avoid a function call when the tracepoint is disabled.
238  */
239 #define DEFINE_RUST_DO_TRACE(name, proto, args)
240 #define __DEFINE_RUST_DO_TRACE(name, proto, args)			\
241 	notrace void rust_do_trace_##name(proto)			\
242 	{								\
243 		__do_trace_##name(args);				\
244 	}
245 
246 /*
247  * When a tracepoint is used, it's name is added to the __tracepoint_check
248  * section. This section is only used at build time to make sure all
249  * defined tracepoints are used. It is discarded after the build.
250  */
251 # define TRACEPOINT_CHECK(name)						\
252 	static const char __used __section("__tracepoint_check")	\
253 	__trace_check_##name[] = #name;
254 
255 /*
256  * Make sure the alignment of the structure in the __tracepoints section will
257  * not add unwanted padding between the beginning of the section and the
258  * structure. Force alignment to the same alignment as the section start.
259  *
260  * When lockdep is enabled, we make sure to always test if RCU is
261  * "watching" regardless if the tracepoint is enabled or not. Tracepoints
262  * require RCU to be active, and it should always warn at the tracepoint
263  * site if it is not watching, as it will need to be active when the
264  * tracepoint is enabled.
265  */
266 #define __DECLARE_TRACE_COMMON(name, proto, args, data_proto)		\
267 	extern int __traceiter_##name(data_proto);			\
268 	DECLARE_STATIC_CALL(tp_func_##name, __traceiter_##name);	\
269 	extern struct tracepoint __tracepoint_##name;			\
270 	extern void rust_do_trace_##name(proto);			\
271 	static inline int						\
272 	register_trace_##name(void (*probe)(data_proto), void *data)	\
273 	{								\
274 		return tracepoint_probe_register(&__tracepoint_##name,	\
275 						(void *)probe, data);	\
276 	}								\
277 	static inline int						\
278 	register_trace_prio_##name(void (*probe)(data_proto), void *data,\
279 				   int prio)				\
280 	{								\
281 		return tracepoint_probe_register_prio(&__tracepoint_##name, \
282 					      (void *)probe, data, prio); \
283 	}								\
284 	static inline int						\
285 	unregister_trace_##name(void (*probe)(data_proto), void *data)	\
286 	{								\
287 		return tracepoint_probe_unregister(&__tracepoint_##name,\
288 						(void *)probe, data);	\
289 	}								\
290 	static inline void						\
291 	check_trace_callback_type_##name(void (*cb)(data_proto))	\
292 	{								\
293 	}								\
294 	static inline bool						\
295 	trace_##name##_enabled(void)					\
296 	{								\
297 		return static_branch_unlikely(&__tracepoint_##name.key);\
298 	}
299 
300 #define __DECLARE_TRACE(name, proto, args, cond, data_proto)			\
301 	__DECLARE_TRACE_COMMON(name, PARAMS(proto), PARAMS(args), PARAMS(data_proto)) \
302 	static inline void __do_trace_##name(proto)			\
303 	{								\
304 		TRACEPOINT_CHECK(name)					\
305 		if (cond) {						\
306 			guard(srcu_fast_notrace)(&tracepoint_srcu);	\
307 			__DO_TRACE_CALL(name, TP_ARGS(args));		\
308 		}							\
309 	}								\
310 	static inline void trace_##name(proto)				\
311 	{								\
312 		if (static_branch_unlikely(&__tracepoint_##name.key))	\
313 			__do_trace_##name(args);			\
314 		if (IS_ENABLED(CONFIG_LOCKDEP) && (cond)) {		\
315 			WARN_ONCE(!rcu_is_watching(),			\
316 				  "RCU not watching for tracepoint");	\
317 		}							\
318 	}								\
319 	static inline void trace_call__##name(proto)			\
320 	{								\
321 		__do_trace_##name(args);				\
322 	}
323 
324 #define __DECLARE_TRACE_SYSCALL(name, proto, args, data_proto)		\
325 	__DECLARE_TRACE_COMMON(name, PARAMS(proto), PARAMS(args), PARAMS(data_proto)) \
326 	static inline void __do_trace_##name(proto)			\
327 	{								\
328 		TRACEPOINT_CHECK(name)					\
329 		guard(rcu_tasks_trace)();				\
330 		__DO_TRACE_CALL(name, TP_ARGS(args));			\
331 	}								\
332 	static inline void trace_##name(proto)				\
333 	{								\
334 		might_fault();						\
335 		if (static_branch_unlikely(&__tracepoint_##name.key))	\
336 			__do_trace_##name(args);			\
337 		if (IS_ENABLED(CONFIG_LOCKDEP)) {			\
338 			WARN_ONCE(!rcu_is_watching(),			\
339 				  "RCU not watching for tracepoint");	\
340 		}							\
341 	}								\
342 	static inline void trace_call__##name(proto)			\
343 	{								\
344 		might_fault();						\
345 		__do_trace_##name(args);				\
346 	}
347 
348 /*
349  * We have no guarantee that gcc and the linker won't up-align the tracepoint
350  * structures, so we create an array of pointers that will be used for iteration
351  * on the tracepoints.
352  *
353  * it_func[0] is never NULL because there is at least one element in the array
354  * when the array itself is non NULL.
355  */
356 #define __DEFINE_TRACE_EXT(_name, _ext, proto, args)			\
357 	static const char __tpstrtab_##_name[]				\
358 	__section("__tracepoints_strings") = #_name;			\
359 	extern struct static_call_key STATIC_CALL_KEY(tp_func_##_name);	\
360 	int __traceiter_##_name(void *__data, proto);			\
361 	void __probestub_##_name(void *__data, proto);			\
362 	struct tracepoint __tracepoint_##_name	__used			\
363 	__section("__tracepoints") = {					\
364 		.name = __tpstrtab_##_name,				\
365 		.key = STATIC_KEY_FALSE_INIT,				\
366 		.static_call_key = &STATIC_CALL_KEY(tp_func_##_name),	\
367 		.static_call_tramp = STATIC_CALL_TRAMP_ADDR(tp_func_##_name), \
368 		.iterator = &__traceiter_##_name,			\
369 		.probestub = &__probestub_##_name,			\
370 		.funcs = NULL,						\
371 		.ext = _ext,						\
372 	};								\
373 	__TRACEPOINT_ENTRY(_name);					\
374 	int __traceiter_##_name(void *__data, proto)			\
375 	{								\
376 		struct tracepoint_func *it_func_ptr;			\
377 		void *it_func;						\
378 									\
379 		it_func_ptr =						\
380 			rcu_dereference_raw((&__tracepoint_##_name)->funcs); \
381 		if (it_func_ptr) {					\
382 			do {						\
383 				it_func = READ_ONCE((it_func_ptr)->func); \
384 				__data = (it_func_ptr)->data;		\
385 				((void(*)(void *, proto))(it_func))(__data, args); \
386 			} while ((++it_func_ptr)->func);		\
387 		}							\
388 		return 0;						\
389 	}								\
390 	void __probestub_##_name(void *__data, proto)			\
391 	{								\
392 	}								\
393 	/*								\
394 	 * Annotate the probestub 'CFI_NOSEAL' to stop objtool from	\
395 	 * requesting the kernel remove the ENDBR, because the only	\
396 	 * references to the function are in the __tracepoint section,	\
397 	 * that objtool doesn't scan.					\
398 	 */								\
399 	CFI_NOSEAL(__probestub_##_name);				\
400 	DEFINE_STATIC_CALL(tp_func_##_name, __traceiter_##_name);	\
401 	DEFINE_RUST_DO_TRACE(_name, TP_PROTO(proto), TP_ARGS(args))
402 
403 #define DEFINE_TRACE_FN(_name, _reg, _unreg, _proto, _args)		\
404 	static struct tracepoint_ext __tracepoint_ext_##_name = {	\
405 		.regfunc = _reg,					\
406 		.unregfunc = _unreg,					\
407 		.faultable = false,					\
408 	};								\
409 	__DEFINE_TRACE_EXT(_name, &__tracepoint_ext_##_name, PARAMS(_proto), PARAMS(_args));
410 
411 #define DEFINE_TRACE_SYSCALL(_name, _reg, _unreg, _proto, _args)	\
412 	static struct tracepoint_ext __tracepoint_ext_##_name = {	\
413 		.regfunc = _reg,					\
414 		.unregfunc = _unreg,					\
415 		.faultable = true,					\
416 	};								\
417 	__DEFINE_TRACE_EXT(_name, &__tracepoint_ext_##_name, PARAMS(_proto), PARAMS(_args));
418 
419 #define DEFINE_TRACE(_name, _proto, _args)				\
420 	__DEFINE_TRACE_EXT(_name, NULL, PARAMS(_proto), PARAMS(_args));
421 
422 #define EXPORT_TRACEPOINT_SYMBOL_GPL(name)				\
423 	TRACEPOINT_CHECK(name)						\
424 	EXPORT_SYMBOL_GPL(__tracepoint_##name);				\
425 	EXPORT_SYMBOL_GPL(__traceiter_##name);				\
426 	EXPORT_STATIC_CALL_GPL(tp_func_##name)
427 #define EXPORT_TRACEPOINT_SYMBOL(name)					\
428 	TRACEPOINT_CHECK(name)						\
429 	EXPORT_SYMBOL(__tracepoint_##name);				\
430 	EXPORT_SYMBOL(__traceiter_##name);				\
431 	EXPORT_STATIC_CALL(tp_func_##name)
432 
433 
434 #else /* !TRACEPOINTS_ENABLED */
435 #define __DECLARE_TRACE_COMMON(name, proto, args, data_proto)		\
436 	static inline void trace_##name(proto)				\
437 	{ }								\
438 	static inline void trace_call__##name(proto)			\
439 	{ }								\
440 	static inline int						\
441 	register_trace_##name(void (*probe)(data_proto),		\
442 			      void *data)				\
443 	{								\
444 		return -ENOSYS;						\
445 	}								\
446 	static inline int						\
447 	unregister_trace_##name(void (*probe)(data_proto),		\
448 				void *data)				\
449 	{								\
450 		return -ENOSYS;						\
451 	}								\
452 	static inline void check_trace_callback_type_##name(void (*cb)(data_proto)) \
453 	{								\
454 	}								\
455 	static inline bool						\
456 	trace_##name##_enabled(void)					\
457 	{								\
458 		return false;						\
459 	}
460 
461 #define __DECLARE_TRACE(name, proto, args, cond, data_proto)		\
462 	__DECLARE_TRACE_COMMON(name, PARAMS(proto), PARAMS(args), PARAMS(data_proto))
463 
464 #define __DECLARE_TRACE_SYSCALL(name, proto, args, data_proto)		\
465 	__DECLARE_TRACE_COMMON(name, PARAMS(proto), PARAMS(args), PARAMS(data_proto))
466 
467 #define DEFINE_TRACE_FN(name, reg, unreg, proto, args)
468 #define DEFINE_TRACE_SYSCALL(name, reg, unreg, proto, args)
469 #define DEFINE_TRACE(name, proto, args)
470 #define EXPORT_TRACEPOINT_SYMBOL_GPL(name)
471 #define EXPORT_TRACEPOINT_SYMBOL(name)
472 
473 #endif /* TRACEPOINTS_ENABLED */
474 
475 #ifdef CONFIG_TRACING
476 /**
477  * tracepoint_string - register constant persistent string to trace system
478  * @str - a constant persistent string that will be referenced in tracepoints
479  *
480  * If constant strings are being used in tracepoints, it is faster and
481  * more efficient to just save the pointer to the string and reference
482  * that with a printf "%s" instead of saving the string in the ring buffer
483  * and wasting space and time.
484  *
485  * The problem with the above approach is that userspace tools that read
486  * the binary output of the trace buffers do not have access to the string.
487  * Instead they just show the address of the string which is not very
488  * useful to users.
489  *
490  * With tracepoint_string(), the string will be registered to the tracing
491  * system and exported to userspace via the debugfs/tracing/printk_formats
492  * file that maps the string address to the string text. This way userspace
493  * tools that read the binary buffers have a way to map the pointers to
494  * the ASCII strings they represent.
495  *
496  * The @str used must be a constant string and persistent as it would not
497  * make sense to show a string that no longer exists. But it is still fine
498  * to be used with modules, because when modules are unloaded, if they
499  * had tracepoints, the ring buffers are cleared too. As long as the string
500  * does not change during the life of the module, it is fine to use
501  * tracepoint_string() within a module.
502  */
503 #define tracepoint_string(str)						\
504 	({								\
505 		static const char *___tp_str __tracepoint_string = str; \
506 		___tp_str;						\
507 	})
508 #define __tracepoint_string	__used __section("__tracepoint_str")
509 #else
510 /*
511  * tracepoint_string() is used to save the string address for userspace
512  * tracing tools. When tracing isn't configured, there's no need to save
513  * anything.
514  */
515 # define tracepoint_string(str) str
516 # define __tracepoint_string
517 #endif
518 
519 #define DECLARE_TRACE(name, proto, args)				\
520 	__DECLARE_TRACE(name##_tp, PARAMS(proto), PARAMS(args),		\
521 			cpu_online(raw_smp_processor_id()),		\
522 			PARAMS(void *__data, proto))
523 
524 #define DECLARE_TRACE_CONDITION(name, proto, args, cond)		\
525 	__DECLARE_TRACE(name##_tp, PARAMS(proto), PARAMS(args),		\
526 			cpu_online(raw_smp_processor_id()) && (PARAMS(cond)), \
527 			PARAMS(void *__data, proto))
528 
529 #define DECLARE_TRACE_SYSCALL(name, proto, args)			\
530 	__DECLARE_TRACE_SYSCALL(name##_tp, PARAMS(proto), PARAMS(args),	\
531 				PARAMS(void *__data, proto))
532 
533 #define DECLARE_TRACE_EVENT(name, proto, args)				\
534 	__DECLARE_TRACE(name, PARAMS(proto), PARAMS(args),		\
535 			cpu_online(raw_smp_processor_id()),		\
536 			PARAMS(void *__data, proto))
537 
538 #define DECLARE_TRACE_EVENT_CONDITION(name, proto, args, cond)		\
539 	__DECLARE_TRACE(name, PARAMS(proto), PARAMS(args),		\
540 			cpu_online(raw_smp_processor_id()) && (PARAMS(cond)), \
541 			PARAMS(void *__data, proto))
542 
543 #define DECLARE_TRACE_EVENT_SYSCALL(name, proto, args)			\
544 	__DECLARE_TRACE_SYSCALL(name, PARAMS(proto), PARAMS(args),	\
545 				PARAMS(void *__data, proto))
546 
547 #define TRACE_EVENT_FLAGS(event, flag)
548 
549 #define TRACE_EVENT_PERF_PERM(event, expr...)
550 
551 #endif /* DECLARE_TRACE */
552 
553 #ifndef TRACE_EVENT
554 /*
555  * For use with the TRACE_EVENT macro:
556  *
557  * We define a tracepoint, its arguments, its printk format
558  * and its 'fast binary record' layout.
559  *
560  * Firstly, name your tracepoint via TRACE_EVENT(name : the
561  * 'subsystem_event' notation is fine.
562  *
563  * Think about this whole construct as the
564  * 'trace_sched_switch() function' from now on.
565  *
566  *
567  *  TRACE_EVENT(sched_switch,
568  *
569  *	*
570  *	* A function has a regular function arguments
571  *	* prototype, declare it via TP_PROTO():
572  *	*
573  *
574  *	TP_PROTO(struct rq *rq, struct task_struct *prev,
575  *		 struct task_struct *next),
576  *
577  *	*
578  *	* Define the call signature of the 'function'.
579  *	* (Design sidenote: we use this instead of a
580  *	*  TP_PROTO1/TP_PROTO2/TP_PROTO3 ugliness.)
581  *	*
582  *
583  *	TP_ARGS(rq, prev, next),
584  *
585  *	*
586  *	* Fast binary tracing: define the trace record via
587  *	* TP_STRUCT__entry(). You can think about it like a
588  *	* regular C structure local variable definition.
589  *	*
590  *	* This is how the trace record is structured and will
591  *	* be saved into the ring buffer. These are the fields
592  *	* that will be exposed to user-space in
593  *	* /sys/kernel/tracing/events/<*>/format.
594  *	*
595  *	* The declared 'local variable' is called '__entry'
596  *	*
597  *	* __field(pid_t, prev_pid) is equivalent to a standard declaration:
598  *	*
599  *	*	pid_t	prev_pid;
600  *	*
601  *	* __array(char, prev_comm, TASK_COMM_LEN) is equivalent to:
602  *	*
603  *	*	char	prev_comm[TASK_COMM_LEN];
604  *	*
605  *
606  *	TP_STRUCT__entry(
607  *		__array(	char,	prev_comm,	TASK_COMM_LEN	)
608  *		__field(	pid_t,	prev_pid			)
609  *		__field(	int,	prev_prio			)
610  *		__array(	char,	next_comm,	TASK_COMM_LEN	)
611  *		__field(	pid_t,	next_pid			)
612  *		__field(	int,	next_prio			)
613  *	),
614  *
615  *	*
616  *	* Assign the entry into the trace record, by embedding
617  *	* a full C statement block into TP_fast_assign(). You
618  *	* can refer to the trace record as '__entry' -
619  *	* otherwise you can put arbitrary C code in here.
620  *	*
621  *	* Note: this C code will execute every time a trace event
622  *	* happens, on an active tracepoint.
623  *	*
624  *
625  *	TP_fast_assign(
626  *		memcpy(__entry->next_comm, next->comm, TASK_COMM_LEN);
627  *		__entry->prev_pid	= prev->pid;
628  *		__entry->prev_prio	= prev->prio;
629  *		memcpy(__entry->prev_comm, prev->comm, TASK_COMM_LEN);
630  *		__entry->next_pid	= next->pid;
631  *		__entry->next_prio	= next->prio;
632  *	),
633  *
634  *	*
635  *	* Formatted output of a trace record via TP_printk().
636  *	* This is how the tracepoint will appear under ftrace
637  *	* plugins that make use of this tracepoint.
638  *	*
639  *	* (raw-binary tracing wont actually perform this step.)
640  *	*
641  *
642  *	TP_printk("task %s:%d [%d] ==> %s:%d [%d]",
643  *		__entry->prev_comm, __entry->prev_pid, __entry->prev_prio,
644  *		__entry->next_comm, __entry->next_pid, __entry->next_prio),
645  *
646  * );
647  *
648  * This macro construct is thus used for the regular printk format
649  * tracing setup, it is used to construct a function pointer based
650  * tracepoint callback (this is used by programmatic plugins and
651  * can also by used by generic instrumentation like SystemTap), and
652  * it is also used to expose a structured trace record in
653  * /sys/kernel/tracing/events/.
654  *
655  * A set of (un)registration functions can be passed to the variant
656  * TRACE_EVENT_FN to perform any (un)registration work.
657  */
658 
659 #define DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, print)
660 #define DEFINE_EVENT(template, name, proto, args)		\
661 	DECLARE_TRACE_EVENT(name, PARAMS(proto), PARAMS(args))
662 #define DEFINE_EVENT_FN(template, name, proto, args, reg, unreg)\
663 	DECLARE_TRACE_EVENT(name, PARAMS(proto), PARAMS(args))
664 #define DEFINE_EVENT_PRINT(template, name, proto, args, print)	\
665 	DECLARE_TRACE_EVENT(name, PARAMS(proto), PARAMS(args))
666 #define DEFINE_EVENT_CONDITION(template, name, proto,		\
667 			       args, cond)			\
668 	DECLARE_TRACE_EVENT_CONDITION(name, PARAMS(proto),	\
669 				PARAMS(args), PARAMS(cond))
670 
671 #define TRACE_EVENT(name, proto, args, struct, assign, print)	\
672 	DECLARE_TRACE_EVENT(name, PARAMS(proto), PARAMS(args))
673 #define TRACE_EVENT_FN(name, proto, args, struct,		\
674 		assign, print, reg, unreg)			\
675 	DECLARE_TRACE_EVENT(name, PARAMS(proto), PARAMS(args))
676 #define TRACE_EVENT_FN_COND(name, proto, args, cond, struct,	\
677 		assign, print, reg, unreg)			\
678 	DECLARE_TRACE_EVENT_CONDITION(name, PARAMS(proto),	\
679 			PARAMS(args), PARAMS(cond))
680 #define TRACE_EVENT_CONDITION(name, proto, args, cond,		\
681 			      struct, assign, print)		\
682 	DECLARE_TRACE_EVENT_CONDITION(name, PARAMS(proto),	\
683 				PARAMS(args), PARAMS(cond))
684 #define TRACE_EVENT_SYSCALL(name, proto, args, struct, assign,	\
685 			    print, reg, unreg)			\
686 	DECLARE_TRACE_EVENT_SYSCALL(name, PARAMS(proto), PARAMS(args))
687 
688 #define TRACE_EVENT_FLAGS(event, flag)
689 
690 #define TRACE_EVENT_PERF_PERM(event, expr...)
691 
692 #define DECLARE_EVENT_NOP(name, proto, args)				\
693 	static inline void trace_##name(proto)				\
694 	{ }								\
695 	static inline bool trace_##name##_enabled(void)			\
696 	{								\
697 		return false;						\
698 	}
699 
700 #define TRACE_EVENT_NOP(name, proto, args, struct, assign, print)	\
701 	DECLARE_EVENT_NOP(name, PARAMS(proto), PARAMS(args))
702 
703 #define DECLARE_EVENT_CLASS_NOP(name, proto, args, tstruct, assign, print)
704 #define DEFINE_EVENT_NOP(template, name, proto, args)			\
705 	DECLARE_EVENT_NOP(name, PARAMS(proto), PARAMS(args))
706 
707 #endif /* ifdef TRACE_EVENT (see note above) */
708