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  * Copyright (C) 2008-2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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/smp.h>
18 #include <linux/errno.h>
19 #include <linux/types.h>
20 #include <linux/cpumask.h>
21 #include <linux/rcupdate.h>
22 #include <linux/static_key.h>
23 
24 struct module;
25 struct tracepoint;
26 struct notifier_block;
27 
28 struct tracepoint_func {
29 	void *func;
30 	void *data;
31 };
32 
33 struct tracepoint {
34 	const char *name;		/* Tracepoint name */
35 	struct static_key key;
36 	void (*regfunc)(void);
37 	void (*unregfunc)(void);
38 	struct tracepoint_func __rcu *funcs;
39 };
40 
41 struct trace_enum_map {
42 	const char		*system;
43 	const char		*enum_string;
44 	unsigned long		enum_value;
45 };
46 
47 extern int
48 tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data);
49 extern int
50 tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data);
51 extern void
52 for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv),
53 		void *priv);
54 
55 #ifdef CONFIG_MODULES
56 struct tp_module {
57 	struct list_head list;
58 	struct module *mod;
59 };
60 
61 bool trace_module_has_bad_taint(struct module *mod);
62 extern int register_tracepoint_module_notifier(struct notifier_block *nb);
63 extern int unregister_tracepoint_module_notifier(struct notifier_block *nb);
64 #else
trace_module_has_bad_taint(struct module * mod)65 static inline bool trace_module_has_bad_taint(struct module *mod)
66 {
67 	return false;
68 }
69 static inline
register_tracepoint_module_notifier(struct notifier_block * nb)70 int register_tracepoint_module_notifier(struct notifier_block *nb)
71 {
72 	return 0;
73 }
74 static inline
unregister_tracepoint_module_notifier(struct notifier_block * nb)75 int unregister_tracepoint_module_notifier(struct notifier_block *nb)
76 {
77 	return 0;
78 }
79 #endif /* CONFIG_MODULES */
80 
81 /*
82  * tracepoint_synchronize_unregister must be called between the last tracepoint
83  * probe unregistration and the end of module exit to make sure there is no
84  * caller executing a probe when it is freed.
85  */
tracepoint_synchronize_unregister(void)86 static inline void tracepoint_synchronize_unregister(void)
87 {
88 	synchronize_sched();
89 }
90 
91 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
92 extern void syscall_regfunc(void);
93 extern void syscall_unregfunc(void);
94 #endif /* CONFIG_HAVE_SYSCALL_TRACEPOINTS */
95 
96 #define PARAMS(args...) args
97 
98 #define TRACE_DEFINE_ENUM(x)
99 
100 #endif /* _LINUX_TRACEPOINT_H */
101 
102 /*
103  * Note: we keep the TRACE_EVENT and DECLARE_TRACE outside the include
104  *  file ifdef protection.
105  *  This is due to the way trace events work. If a file includes two
106  *  trace event headers under one "CREATE_TRACE_POINTS" the first include
107  *  will override the TRACE_EVENT and break the second include.
108  */
109 
110 #ifndef DECLARE_TRACE
111 
112 #define TP_PROTO(args...)	args
113 #define TP_ARGS(args...)	args
114 #define TP_CONDITION(args...)	args
115 
116 #ifdef CONFIG_TRACEPOINTS
117 
118 /*
119  * it_func[0] is never NULL because there is at least one element in the array
120  * when the array itself is non NULL.
121  *
122  * Note, the proto and args passed in includes "__data" as the first parameter.
123  * The reason for this is to handle the "void" prototype. If a tracepoint
124  * has a "void" prototype, then it is invalid to declare a function
125  * as "(void *, void)". The DECLARE_TRACE_NOARGS() will pass in just
126  * "void *data", where as the DECLARE_TRACE() will pass in "void *data, proto".
127  */
128 #define __DO_TRACE(tp, proto, args, cond, prercu, postrcu)		\
129 	do {								\
130 		struct tracepoint_func *it_func_ptr;			\
131 		void *it_func;						\
132 		void *__data;						\
133 									\
134 		if (!(cond))						\
135 			return;						\
136 		prercu;							\
137 		rcu_read_lock_sched_notrace();				\
138 		it_func_ptr = rcu_dereference_sched((tp)->funcs);	\
139 		if (it_func_ptr) {					\
140 			do {						\
141 				it_func = (it_func_ptr)->func;		\
142 				__data = (it_func_ptr)->data;		\
143 				((void(*)(proto))(it_func))(args);	\
144 			} while ((++it_func_ptr)->func);		\
145 		}							\
146 		rcu_read_unlock_sched_notrace();			\
147 		postrcu;						\
148 	} while (0)
149 
150 #ifndef MODULE
151 #define __DECLARE_TRACE_RCU(name, proto, args, cond, data_proto, data_args)	\
152 	static inline void trace_##name##_rcuidle(proto)		\
153 	{								\
154 		if (static_key_false(&__tracepoint_##name.key))		\
155 			__DO_TRACE(&__tracepoint_##name,		\
156 				TP_PROTO(data_proto),			\
157 				TP_ARGS(data_args),			\
158 				TP_CONDITION(cond),			\
159 				rcu_irq_enter(),			\
160 				rcu_irq_exit());			\
161 	}
162 #else
163 #define __DECLARE_TRACE_RCU(name, proto, args, cond, data_proto, data_args)
164 #endif
165 
166 /*
167  * Make sure the alignment of the structure in the __tracepoints section will
168  * not add unwanted padding between the beginning of the section and the
169  * structure. Force alignment to the same alignment as the section start.
170  *
171  * When lockdep is enabled, we make sure to always do the RCU portions of
172  * the tracepoint code, regardless of whether tracing is on or we match the
173  * condition.  This lets us find RCU issues triggered with tracepoints even
174  * when this tracepoint is off.  This code has no purpose other than poking
175  * RCU a bit.
176  */
177 #define __DECLARE_TRACE(name, proto, args, cond, data_proto, data_args) \
178 	extern struct tracepoint __tracepoint_##name;			\
179 	static inline void trace_##name(proto)				\
180 	{								\
181 		if (static_key_false(&__tracepoint_##name.key))		\
182 			__DO_TRACE(&__tracepoint_##name,		\
183 				TP_PROTO(data_proto),			\
184 				TP_ARGS(data_args),			\
185 				TP_CONDITION(cond),,);			\
186 		if (IS_ENABLED(CONFIG_LOCKDEP) && (cond)) {		\
187 			rcu_read_lock_sched_notrace();			\
188 			rcu_dereference_sched(__tracepoint_##name.funcs);\
189 			rcu_read_unlock_sched_notrace();		\
190 		}							\
191 	}								\
192 	__DECLARE_TRACE_RCU(name, PARAMS(proto), PARAMS(args),		\
193 		PARAMS(cond), PARAMS(data_proto), PARAMS(data_args))	\
194 	static inline int						\
195 	register_trace_##name(void (*probe)(data_proto), void *data)	\
196 	{								\
197 		return tracepoint_probe_register(&__tracepoint_##name,	\
198 						(void *)probe, data);	\
199 	}								\
200 	static inline int						\
201 	unregister_trace_##name(void (*probe)(data_proto), void *data)	\
202 	{								\
203 		return tracepoint_probe_unregister(&__tracepoint_##name,\
204 						(void *)probe, data);	\
205 	}								\
206 	static inline void						\
207 	check_trace_callback_type_##name(void (*cb)(data_proto))	\
208 	{								\
209 	}								\
210 	static inline bool						\
211 	trace_##name##_enabled(void)					\
212 	{								\
213 		return static_key_false(&__tracepoint_##name.key);	\
214 	}
215 
216 /*
217  * We have no guarantee that gcc and the linker won't up-align the tracepoint
218  * structures, so we create an array of pointers that will be used for iteration
219  * on the tracepoints.
220  */
221 #define DEFINE_TRACE_FN(name, reg, unreg)				 \
222 	static const char __tpstrtab_##name[]				 \
223 	__attribute__((section("__tracepoints_strings"))) = #name;	 \
224 	struct tracepoint __tracepoint_##name				 \
225 	__attribute__((section("__tracepoints"))) =			 \
226 		{ __tpstrtab_##name, STATIC_KEY_INIT_FALSE, reg, unreg, NULL };\
227 	static struct tracepoint * const __tracepoint_ptr_##name __used	 \
228 	__attribute__((section("__tracepoints_ptrs"))) =		 \
229 		&__tracepoint_##name;
230 
231 #define DEFINE_TRACE(name)						\
232 	DEFINE_TRACE_FN(name, NULL, NULL);
233 
234 #define EXPORT_TRACEPOINT_SYMBOL_GPL(name)				\
235 	EXPORT_SYMBOL_GPL(__tracepoint_##name)
236 #define EXPORT_TRACEPOINT_SYMBOL(name)					\
237 	EXPORT_SYMBOL(__tracepoint_##name)
238 
239 #else /* !CONFIG_TRACEPOINTS */
240 #define __DECLARE_TRACE(name, proto, args, cond, data_proto, data_args) \
241 	static inline void trace_##name(proto)				\
242 	{ }								\
243 	static inline void trace_##name##_rcuidle(proto)		\
244 	{ }								\
245 	static inline int						\
246 	register_trace_##name(void (*probe)(data_proto),		\
247 			      void *data)				\
248 	{								\
249 		return -ENOSYS;						\
250 	}								\
251 	static inline int						\
252 	unregister_trace_##name(void (*probe)(data_proto),		\
253 				void *data)				\
254 	{								\
255 		return -ENOSYS;						\
256 	}								\
257 	static inline void check_trace_callback_type_##name(void (*cb)(data_proto)) \
258 	{								\
259 	}								\
260 	static inline bool						\
261 	trace_##name##_enabled(void)					\
262 	{								\
263 		return false;						\
264 	}
265 
266 #define DEFINE_TRACE_FN(name, reg, unreg)
267 #define DEFINE_TRACE(name)
268 #define EXPORT_TRACEPOINT_SYMBOL_GPL(name)
269 #define EXPORT_TRACEPOINT_SYMBOL(name)
270 
271 #endif /* CONFIG_TRACEPOINTS */
272 
273 #ifdef CONFIG_TRACING
274 /**
275  * tracepoint_string - register constant persistent string to trace system
276  * @str - a constant persistent string that will be referenced in tracepoints
277  *
278  * If constant strings are being used in tracepoints, it is faster and
279  * more efficient to just save the pointer to the string and reference
280  * that with a printf "%s" instead of saving the string in the ring buffer
281  * and wasting space and time.
282  *
283  * The problem with the above approach is that userspace tools that read
284  * the binary output of the trace buffers do not have access to the string.
285  * Instead they just show the address of the string which is not very
286  * useful to users.
287  *
288  * With tracepoint_string(), the string will be registered to the tracing
289  * system and exported to userspace via the debugfs/tracing/printk_formats
290  * file that maps the string address to the string text. This way userspace
291  * tools that read the binary buffers have a way to map the pointers to
292  * the ASCII strings they represent.
293  *
294  * The @str used must be a constant string and persistent as it would not
295  * make sense to show a string that no longer exists. But it is still fine
296  * to be used with modules, because when modules are unloaded, if they
297  * had tracepoints, the ring buffers are cleared too. As long as the string
298  * does not change during the life of the module, it is fine to use
299  * tracepoint_string() within a module.
300  */
301 #define tracepoint_string(str)						\
302 	({								\
303 		static const char *___tp_str __tracepoint_string = str; \
304 		___tp_str;						\
305 	})
306 #define __tracepoint_string	__attribute__((section("__tracepoint_str")))
307 #else
308 /*
309  * tracepoint_string() is used to save the string address for userspace
310  * tracing tools. When tracing isn't configured, there's no need to save
311  * anything.
312  */
313 # define tracepoint_string(str) str
314 # define __tracepoint_string
315 #endif
316 
317 /*
318  * The need for the DECLARE_TRACE_NOARGS() is to handle the prototype
319  * (void). "void" is a special value in a function prototype and can
320  * not be combined with other arguments. Since the DECLARE_TRACE()
321  * macro adds a data element at the beginning of the prototype,
322  * we need a way to differentiate "(void *data, proto)" from
323  * "(void *data, void)". The second prototype is invalid.
324  *
325  * DECLARE_TRACE_NOARGS() passes "void" as the tracepoint prototype
326  * and "void *__data" as the callback prototype.
327  *
328  * DECLARE_TRACE() passes "proto" as the tracepoint protoype and
329  * "void *__data, proto" as the callback prototype.
330  */
331 #define DECLARE_TRACE_NOARGS(name)					\
332 	__DECLARE_TRACE(name, void, ,					\
333 			cpu_online(raw_smp_processor_id()),		\
334 			void *__data, __data)
335 
336 #define DECLARE_TRACE(name, proto, args)				\
337 	__DECLARE_TRACE(name, PARAMS(proto), PARAMS(args),		\
338 			cpu_online(raw_smp_processor_id()),		\
339 			PARAMS(void *__data, proto),			\
340 			PARAMS(__data, args))
341 
342 #define DECLARE_TRACE_CONDITION(name, proto, args, cond)		\
343 	__DECLARE_TRACE(name, PARAMS(proto), PARAMS(args),		\
344 			cpu_online(raw_smp_processor_id()) && (PARAMS(cond)), \
345 			PARAMS(void *__data, proto),			\
346 			PARAMS(__data, args))
347 
348 #define TRACE_EVENT_FLAGS(event, flag)
349 
350 #define TRACE_EVENT_PERF_PERM(event, expr...)
351 
352 #endif /* DECLARE_TRACE */
353 
354 #ifndef TRACE_EVENT
355 /*
356  * For use with the TRACE_EVENT macro:
357  *
358  * We define a tracepoint, its arguments, its printk format
359  * and its 'fast binary record' layout.
360  *
361  * Firstly, name your tracepoint via TRACE_EVENT(name : the
362  * 'subsystem_event' notation is fine.
363  *
364  * Think about this whole construct as the
365  * 'trace_sched_switch() function' from now on.
366  *
367  *
368  *  TRACE_EVENT(sched_switch,
369  *
370  *	*
371  *	* A function has a regular function arguments
372  *	* prototype, declare it via TP_PROTO():
373  *	*
374  *
375  *	TP_PROTO(struct rq *rq, struct task_struct *prev,
376  *		 struct task_struct *next),
377  *
378  *	*
379  *	* Define the call signature of the 'function'.
380  *	* (Design sidenote: we use this instead of a
381  *	*  TP_PROTO1/TP_PROTO2/TP_PROTO3 ugliness.)
382  *	*
383  *
384  *	TP_ARGS(rq, prev, next),
385  *
386  *	*
387  *	* Fast binary tracing: define the trace record via
388  *	* TP_STRUCT__entry(). You can think about it like a
389  *	* regular C structure local variable definition.
390  *	*
391  *	* This is how the trace record is structured and will
392  *	* be saved into the ring buffer. These are the fields
393  *	* that will be exposed to user-space in
394  *	* /sys/kernel/debug/tracing/events/<*>/format.
395  *	*
396  *	* The declared 'local variable' is called '__entry'
397  *	*
398  *	* __field(pid_t, prev_prid) is equivalent to a standard declariton:
399  *	*
400  *	*	pid_t	prev_pid;
401  *	*
402  *	* __array(char, prev_comm, TASK_COMM_LEN) is equivalent to:
403  *	*
404  *	*	char	prev_comm[TASK_COMM_LEN];
405  *	*
406  *
407  *	TP_STRUCT__entry(
408  *		__array(	char,	prev_comm,	TASK_COMM_LEN	)
409  *		__field(	pid_t,	prev_pid			)
410  *		__field(	int,	prev_prio			)
411  *		__array(	char,	next_comm,	TASK_COMM_LEN	)
412  *		__field(	pid_t,	next_pid			)
413  *		__field(	int,	next_prio			)
414  *	),
415  *
416  *	*
417  *	* Assign the entry into the trace record, by embedding
418  *	* a full C statement block into TP_fast_assign(). You
419  *	* can refer to the trace record as '__entry' -
420  *	* otherwise you can put arbitrary C code in here.
421  *	*
422  *	* Note: this C code will execute every time a trace event
423  *	* happens, on an active tracepoint.
424  *	*
425  *
426  *	TP_fast_assign(
427  *		memcpy(__entry->next_comm, next->comm, TASK_COMM_LEN);
428  *		__entry->prev_pid	= prev->pid;
429  *		__entry->prev_prio	= prev->prio;
430  *		memcpy(__entry->prev_comm, prev->comm, TASK_COMM_LEN);
431  *		__entry->next_pid	= next->pid;
432  *		__entry->next_prio	= next->prio;
433  *	),
434  *
435  *	*
436  *	* Formatted output of a trace record via TP_printk().
437  *	* This is how the tracepoint will appear under ftrace
438  *	* plugins that make use of this tracepoint.
439  *	*
440  *	* (raw-binary tracing wont actually perform this step.)
441  *	*
442  *
443  *	TP_printk("task %s:%d [%d] ==> %s:%d [%d]",
444  *		__entry->prev_comm, __entry->prev_pid, __entry->prev_prio,
445  *		__entry->next_comm, __entry->next_pid, __entry->next_prio),
446  *
447  * );
448  *
449  * This macro construct is thus used for the regular printk format
450  * tracing setup, it is used to construct a function pointer based
451  * tracepoint callback (this is used by programmatic plugins and
452  * can also by used by generic instrumentation like SystemTap), and
453  * it is also used to expose a structured trace record in
454  * /sys/kernel/debug/tracing/events/.
455  *
456  * A set of (un)registration functions can be passed to the variant
457  * TRACE_EVENT_FN to perform any (un)registration work.
458  */
459 
460 #define DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, print)
461 #define DEFINE_EVENT(template, name, proto, args)		\
462 	DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
463 #define DEFINE_EVENT_FN(template, name, proto, args, reg, unreg)\
464 	DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
465 #define DEFINE_EVENT_PRINT(template, name, proto, args, print)	\
466 	DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
467 #define DEFINE_EVENT_CONDITION(template, name, proto,		\
468 			       args, cond)			\
469 	DECLARE_TRACE_CONDITION(name, PARAMS(proto),		\
470 				PARAMS(args), PARAMS(cond))
471 
472 #define TRACE_EVENT(name, proto, args, struct, assign, print)	\
473 	DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
474 #define TRACE_EVENT_FN(name, proto, args, struct,		\
475 		assign, print, reg, unreg)			\
476 	DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
477 #define TRACE_EVENT_CONDITION(name, proto, args, cond,		\
478 			      struct, assign, print)		\
479 	DECLARE_TRACE_CONDITION(name, PARAMS(proto),		\
480 				PARAMS(args), PARAMS(cond))
481 
482 #define TRACE_EVENT_FLAGS(event, flag)
483 
484 #define TRACE_EVENT_PERF_PERM(event, expr...)
485 
486 #endif /* ifdef TRACE_EVENT (see note above) */
487