xref: /linux-6.15/include/linux/tracepoint.h (revision c145211d)
1 #ifndef _LINUX_TRACEPOINT_H
2 #define _LINUX_TRACEPOINT_H
3 
4 /*
5  * Kernel Tracepoint API.
6  *
7  * See Documentation/trace/tracepoints.txt.
8  *
9  * (C) Copyright 2008 Mathieu Desnoyers <[email protected]>
10  *
11  * Heavily inspired from the Linux Kernel Markers.
12  *
13  * This file is released under the GPLv2.
14  * See the file COPYING for more details.
15  */
16 
17 #include <linux/types.h>
18 #include <linux/rcupdate.h>
19 
20 struct module;
21 struct tracepoint;
22 
23 struct tracepoint {
24 	const char *name;		/* Tracepoint name */
25 	int state;			/* State. */
26 	void (*regfunc)(void);
27 	void (*unregfunc)(void);
28 	void **funcs;
29 } __attribute__((aligned(32)));		/*
30 					 * Aligned on 32 bytes because it is
31 					 * globally visible and gcc happily
32 					 * align these on the structure size.
33 					 * Keep in sync with vmlinux.lds.h.
34 					 */
35 
36 /*
37  * Connect a probe to a tracepoint.
38  * Internal API, should not be used directly.
39  */
40 extern int tracepoint_probe_register(const char *name, void *probe);
41 
42 /*
43  * Disconnect a probe from a tracepoint.
44  * Internal API, should not be used directly.
45  */
46 extern int tracepoint_probe_unregister(const char *name, void *probe);
47 
48 extern int tracepoint_probe_register_noupdate(const char *name, void *probe);
49 extern int tracepoint_probe_unregister_noupdate(const char *name, void *probe);
50 extern void tracepoint_probe_update_all(void);
51 
52 struct tracepoint_iter {
53 	struct module *module;
54 	struct tracepoint *tracepoint;
55 };
56 
57 extern void tracepoint_iter_start(struct tracepoint_iter *iter);
58 extern void tracepoint_iter_next(struct tracepoint_iter *iter);
59 extern void tracepoint_iter_stop(struct tracepoint_iter *iter);
60 extern void tracepoint_iter_reset(struct tracepoint_iter *iter);
61 extern int tracepoint_get_iter_range(struct tracepoint **tracepoint,
62 	struct tracepoint *begin, struct tracepoint *end);
63 
64 /*
65  * tracepoint_synchronize_unregister must be called between the last tracepoint
66  * probe unregistration and the end of module exit to make sure there is no
67  * caller executing a probe when it is freed.
68  */
69 static inline void tracepoint_synchronize_unregister(void)
70 {
71 	synchronize_sched();
72 }
73 
74 #define PARAMS(args...) args
75 
76 #ifdef CONFIG_TRACEPOINTS
77 extern void tracepoint_update_probe_range(struct tracepoint *begin,
78 	struct tracepoint *end);
79 #else
80 static inline void tracepoint_update_probe_range(struct tracepoint *begin,
81 	struct tracepoint *end)
82 { }
83 #endif /* CONFIG_TRACEPOINTS */
84 
85 #endif /* _LINUX_TRACEPOINT_H */
86 
87 /*
88  * Note: we keep the TRACE_EVENT and DECLARE_TRACE outside the include
89  *  file ifdef protection.
90  *  This is due to the way trace events work. If a file includes two
91  *  trace event headers under one "CREATE_TRACE_POINTS" the first include
92  *  will override the TRACE_EVENT and break the second include.
93  */
94 
95 #ifndef DECLARE_TRACE
96 
97 #define TP_PROTO(args...)	args
98 #define TP_ARGS(args...)	args
99 
100 #ifdef CONFIG_TRACEPOINTS
101 
102 /*
103  * it_func[0] is never NULL because there is at least one element in the array
104  * when the array itself is non NULL.
105  */
106 #define __DO_TRACE(tp, proto, args)					\
107 	do {								\
108 		void **it_func;						\
109 									\
110 		rcu_read_lock_sched_notrace();				\
111 		it_func = rcu_dereference_sched((tp)->funcs);		\
112 		if (it_func) {						\
113 			do {						\
114 				((void(*)(proto))(*it_func))(args);	\
115 			} while (*(++it_func));				\
116 		}							\
117 		rcu_read_unlock_sched_notrace();			\
118 	} while (0)
119 
120 /*
121  * Make sure the alignment of the structure in the __tracepoints section will
122  * not add unwanted padding between the beginning of the section and the
123  * structure. Force alignment to the same alignment as the section start.
124  */
125 #define DECLARE_TRACE(name, proto, args)				\
126 	extern struct tracepoint __tracepoint_##name;			\
127 	static inline void trace_##name(proto)				\
128 	{								\
129 		if (unlikely(__tracepoint_##name.state))		\
130 			__DO_TRACE(&__tracepoint_##name,		\
131 				TP_PROTO(proto), TP_ARGS(args));	\
132 	}								\
133 	static inline int register_trace_##name(void (*probe)(proto))	\
134 	{								\
135 		return tracepoint_probe_register(#name, (void *)probe);	\
136 	}								\
137 	static inline int unregister_trace_##name(void (*probe)(proto))	\
138 	{								\
139 		return tracepoint_probe_unregister(#name, (void *)probe);\
140 	}
141 
142 
143 #define DEFINE_TRACE_FN(name, reg, unreg)				\
144 	static const char __tpstrtab_##name[]				\
145 	__attribute__((section("__tracepoints_strings"))) = #name;	\
146 	struct tracepoint __tracepoint_##name				\
147 	__attribute__((section("__tracepoints"), aligned(32))) =	\
148 		{ __tpstrtab_##name, 0, reg, unreg, NULL }
149 
150 #define DEFINE_TRACE(name)						\
151 	DEFINE_TRACE_FN(name, NULL, NULL);
152 
153 #define EXPORT_TRACEPOINT_SYMBOL_GPL(name)				\
154 	EXPORT_SYMBOL_GPL(__tracepoint_##name)
155 #define EXPORT_TRACEPOINT_SYMBOL(name)					\
156 	EXPORT_SYMBOL(__tracepoint_##name)
157 
158 #else /* !CONFIG_TRACEPOINTS */
159 #define DECLARE_TRACE(name, proto, args)				\
160 	static inline void _do_trace_##name(struct tracepoint *tp, proto) \
161 	{ }								\
162 	static inline void trace_##name(proto)				\
163 	{ }								\
164 	static inline int register_trace_##name(void (*probe)(proto))	\
165 	{								\
166 		return -ENOSYS;						\
167 	}								\
168 	static inline int unregister_trace_##name(void (*probe)(proto))	\
169 	{								\
170 		return -ENOSYS;						\
171 	}
172 
173 #define DEFINE_TRACE_FN(name, reg, unreg)
174 #define DEFINE_TRACE(name)
175 #define EXPORT_TRACEPOINT_SYMBOL_GPL(name)
176 #define EXPORT_TRACEPOINT_SYMBOL(name)
177 
178 #endif /* CONFIG_TRACEPOINTS */
179 #endif /* DECLARE_TRACE */
180 
181 #ifndef TRACE_EVENT
182 /*
183  * For use with the TRACE_EVENT macro:
184  *
185  * We define a tracepoint, its arguments, its printk format
186  * and its 'fast binay record' layout.
187  *
188  * Firstly, name your tracepoint via TRACE_EVENT(name : the
189  * 'subsystem_event' notation is fine.
190  *
191  * Think about this whole construct as the
192  * 'trace_sched_switch() function' from now on.
193  *
194  *
195  *  TRACE_EVENT(sched_switch,
196  *
197  *	*
198  *	* A function has a regular function arguments
199  *	* prototype, declare it via TP_PROTO():
200  *	*
201  *
202  *	TP_PROTO(struct rq *rq, struct task_struct *prev,
203  *		 struct task_struct *next),
204  *
205  *	*
206  *	* Define the call signature of the 'function'.
207  *	* (Design sidenote: we use this instead of a
208  *	*  TP_PROTO1/TP_PROTO2/TP_PROTO3 ugliness.)
209  *	*
210  *
211  *	TP_ARGS(rq, prev, next),
212  *
213  *	*
214  *	* Fast binary tracing: define the trace record via
215  *	* TP_STRUCT__entry(). You can think about it like a
216  *	* regular C structure local variable definition.
217  *	*
218  *	* This is how the trace record is structured and will
219  *	* be saved into the ring buffer. These are the fields
220  *	* that will be exposed to user-space in
221  *	* /sys/kernel/debug/tracing/events/<*>/format.
222  *	*
223  *	* The declared 'local variable' is called '__entry'
224  *	*
225  *	* __field(pid_t, prev_prid) is equivalent to a standard declariton:
226  *	*
227  *	*	pid_t	prev_pid;
228  *	*
229  *	* __array(char, prev_comm, TASK_COMM_LEN) is equivalent to:
230  *	*
231  *	*	char	prev_comm[TASK_COMM_LEN];
232  *	*
233  *
234  *	TP_STRUCT__entry(
235  *		__array(	char,	prev_comm,	TASK_COMM_LEN	)
236  *		__field(	pid_t,	prev_pid			)
237  *		__field(	int,	prev_prio			)
238  *		__array(	char,	next_comm,	TASK_COMM_LEN	)
239  *		__field(	pid_t,	next_pid			)
240  *		__field(	int,	next_prio			)
241  *	),
242  *
243  *	*
244  *	* Assign the entry into the trace record, by embedding
245  *	* a full C statement block into TP_fast_assign(). You
246  *	* can refer to the trace record as '__entry' -
247  *	* otherwise you can put arbitrary C code in here.
248  *	*
249  *	* Note: this C code will execute every time a trace event
250  *	* happens, on an active tracepoint.
251  *	*
252  *
253  *	TP_fast_assign(
254  *		memcpy(__entry->next_comm, next->comm, TASK_COMM_LEN);
255  *		__entry->prev_pid	= prev->pid;
256  *		__entry->prev_prio	= prev->prio;
257  *		memcpy(__entry->prev_comm, prev->comm, TASK_COMM_LEN);
258  *		__entry->next_pid	= next->pid;
259  *		__entry->next_prio	= next->prio;
260  *	)
261  *
262  *	*
263  *	* Formatted output of a trace record via TP_printk().
264  *	* This is how the tracepoint will appear under ftrace
265  *	* plugins that make use of this tracepoint.
266  *	*
267  *	* (raw-binary tracing wont actually perform this step.)
268  *	*
269  *
270  *	TP_printk("task %s:%d [%d] ==> %s:%d [%d]",
271  *		__entry->prev_comm, __entry->prev_pid, __entry->prev_prio,
272  *		__entry->next_comm, __entry->next_pid, __entry->next_prio),
273  *
274  * );
275  *
276  * This macro construct is thus used for the regular printk format
277  * tracing setup, it is used to construct a function pointer based
278  * tracepoint callback (this is used by programmatic plugins and
279  * can also by used by generic instrumentation like SystemTap), and
280  * it is also used to expose a structured trace record in
281  * /sys/kernel/debug/tracing/events/.
282  *
283  * A set of (un)registration functions can be passed to the variant
284  * TRACE_EVENT_FN to perform any (un)registration work.
285  */
286 
287 #define DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, print)
288 #define DEFINE_EVENT(template, name, proto, args)		\
289 	DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
290 #define DEFINE_EVENT_PRINT(template, name, proto, args, print)	\
291 	DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
292 
293 #define TRACE_EVENT(name, proto, args, struct, assign, print)	\
294 	DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
295 #define TRACE_EVENT_FN(name, proto, args, struct,		\
296 		assign, print, reg, unreg)			\
297 	DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
298 
299 #endif /* ifdef TRACE_EVENT (see note above) */
300