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