1 #ifndef _LINUX_TRACEPOINT_H
2 #define _LINUX_TRACEPOINT_H
5 * Kernel Tracepoint API.
7 * See Documentation/trace/tracepoints.txt.
9 * (C) Copyright 2008 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
11 * Heavily inspired from the Linux Kernel Markers.
13 * This file is released under the GPLv2.
14 * See the file COPYING for more details.
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/jump_label.h>
27 struct tracepoint_func {
33 const char *name; /* Tracepoint name */
34 struct jump_label_key key;
35 void (*regfunc)(void);
36 void (*unregfunc)(void);
37 struct tracepoint_func __rcu *funcs;
41 * Connect a probe to a tracepoint.
42 * Internal API, should not be used directly.
44 extern int tracepoint_probe_register(const char *name, void *probe, void *data);
47 * Disconnect a probe from a tracepoint.
48 * Internal API, should not be used directly.
51 tracepoint_probe_unregister(const char *name, void *probe, void *data);
53 extern int tracepoint_probe_register_noupdate(const char *name, void *probe,
55 extern int tracepoint_probe_unregister_noupdate(const char *name, void *probe,
57 extern void tracepoint_probe_update_all(void);
61 struct list_head list;
62 unsigned int num_tracepoints;
63 struct tracepoint * const *tracepoints_ptrs;
65 bool trace_module_has_bad_taint(struct module *mod);
67 static inline bool trace_module_has_bad_taint(struct module *mod)
71 #endif /* CONFIG_MODULES */
73 struct tracepoint_iter {
75 struct tp_module *module;
76 #endif /* CONFIG_MODULES */
77 struct tracepoint * const *tracepoint;
80 extern void tracepoint_iter_start(struct tracepoint_iter *iter);
81 extern void tracepoint_iter_next(struct tracepoint_iter *iter);
82 extern void tracepoint_iter_stop(struct tracepoint_iter *iter);
83 extern void tracepoint_iter_reset(struct tracepoint_iter *iter);
86 * tracepoint_synchronize_unregister must be called between the last tracepoint
87 * probe unregistration and the end of module exit to make sure there is no
88 * caller executing a probe when it is freed.
90 static inline void tracepoint_synchronize_unregister(void)
95 #define PARAMS(args...) args
97 #endif /* _LINUX_TRACEPOINT_H */
100 * Note: we keep the TRACE_EVENT and DECLARE_TRACE outside the include
101 * file ifdef protection.
102 * This is due to the way trace events work. If a file includes two
103 * trace event headers under one "CREATE_TRACE_POINTS" the first include
104 * will override the TRACE_EVENT and break the second include.
107 #ifndef DECLARE_TRACE
109 #define TP_PROTO(args...) args
110 #define TP_ARGS(args...) args
111 #define TP_CONDITION(args...) args
113 #ifdef CONFIG_TRACEPOINTS
116 * it_func[0] is never NULL because there is at least one element in the array
117 * when the array itself is non NULL.
119 * Note, the proto and args passed in includes "__data" as the first parameter.
120 * The reason for this is to handle the "void" prototype. If a tracepoint
121 * has a "void" prototype, then it is invalid to declare a function
122 * as "(void *, void)". The DECLARE_TRACE_NOARGS() will pass in just
123 * "void *data", where as the DECLARE_TRACE() will pass in "void *data, proto".
125 #define __DO_TRACE(tp, proto, args, cond) \
127 struct tracepoint_func *it_func_ptr; \
131 if (!cpu_online(raw_smp_processor_id())) \
136 rcu_read_lock_sched_notrace(); \
137 it_func_ptr = rcu_dereference_sched((tp)->funcs); \
140 it_func = (it_func_ptr)->func; \
141 __data = (it_func_ptr)->data; \
142 ((void(*)(proto))(it_func))(args); \
143 } while ((++it_func_ptr)->func); \
145 rcu_read_unlock_sched_notrace(); \
149 * Make sure the alignment of the structure in the __tracepoints section will
150 * not add unwanted padding between the beginning of the section and the
151 * structure. Force alignment to the same alignment as the section start.
153 #define __DECLARE_TRACE(name, proto, args, cond, data_proto, data_args) \
154 extern struct tracepoint __tracepoint_##name; \
155 static inline void trace_##name(proto) \
157 if (static_branch(&__tracepoint_##name.key)) \
158 __DO_TRACE(&__tracepoint_##name, \
159 TP_PROTO(data_proto), \
160 TP_ARGS(data_args), \
161 TP_CONDITION(cond)); \
164 register_trace_##name(void (*probe)(data_proto), void *data) \
166 return tracepoint_probe_register(#name, (void *)probe, \
170 unregister_trace_##name(void (*probe)(data_proto), void *data) \
172 return tracepoint_probe_unregister(#name, (void *)probe, \
176 check_trace_callback_type_##name(void (*cb)(data_proto)) \
181 * We have no guarantee that gcc and the linker won't up-align the tracepoint
182 * structures, so we create an array of pointers that will be used for iteration
183 * on the tracepoints.
185 #define DEFINE_TRACE_FN(name, reg, unreg) \
186 static const char __tpstrtab_##name[] \
187 __attribute__((section("__tracepoints_strings"))) = #name; \
188 struct tracepoint __tracepoint_##name \
189 __attribute__((section("__tracepoints"))) = \
190 { __tpstrtab_##name, JUMP_LABEL_INIT, reg, unreg, NULL };\
191 static struct tracepoint * const __tracepoint_ptr_##name __used \
192 __attribute__((section("__tracepoints_ptrs"))) = \
193 &__tracepoint_##name;
195 #define DEFINE_TRACE(name) \
196 DEFINE_TRACE_FN(name, NULL, NULL);
198 #define EXPORT_TRACEPOINT_SYMBOL_GPL(name) \
199 EXPORT_SYMBOL_GPL(__tracepoint_##name)
200 #define EXPORT_TRACEPOINT_SYMBOL(name) \
201 EXPORT_SYMBOL(__tracepoint_##name)
203 #else /* !CONFIG_TRACEPOINTS */
204 #define __DECLARE_TRACE(name, proto, args, cond, data_proto, data_args) \
205 static inline void trace_##name(proto) \
208 register_trace_##name(void (*probe)(data_proto), \
214 unregister_trace_##name(void (*probe)(data_proto), \
219 static inline void check_trace_callback_type_##name(void (*cb)(data_proto)) \
223 #define DEFINE_TRACE_FN(name, reg, unreg)
224 #define DEFINE_TRACE(name)
225 #define EXPORT_TRACEPOINT_SYMBOL_GPL(name)
226 #define EXPORT_TRACEPOINT_SYMBOL(name)
228 #endif /* CONFIG_TRACEPOINTS */
231 * The need for the DECLARE_TRACE_NOARGS() is to handle the prototype
232 * (void). "void" is a special value in a function prototype and can
233 * not be combined with other arguments. Since the DECLARE_TRACE()
234 * macro adds a data element at the beginning of the prototype,
235 * we need a way to differentiate "(void *data, proto)" from
236 * "(void *data, void)". The second prototype is invalid.
238 * DECLARE_TRACE_NOARGS() passes "void" as the tracepoint prototype
239 * and "void *__data" as the callback prototype.
241 * DECLARE_TRACE() passes "proto" as the tracepoint protoype and
242 * "void *__data, proto" as the callback prototype.
244 #define DECLARE_TRACE_NOARGS(name) \
245 __DECLARE_TRACE(name, void, , 1, void *__data, __data)
247 #define DECLARE_TRACE(name, proto, args) \
248 __DECLARE_TRACE(name, PARAMS(proto), PARAMS(args), 1, \
249 PARAMS(void *__data, proto), \
250 PARAMS(__data, args))
252 #define DECLARE_TRACE_CONDITION(name, proto, args, cond) \
253 __DECLARE_TRACE(name, PARAMS(proto), PARAMS(args), PARAMS(cond), \
254 PARAMS(void *__data, proto), \
255 PARAMS(__data, args))
257 #define TRACE_EVENT_FLAGS(event, flag)
259 #endif /* DECLARE_TRACE */
263 * For use with the TRACE_EVENT macro:
265 * We define a tracepoint, its arguments, its printk format
266 * and its 'fast binay record' layout.
268 * Firstly, name your tracepoint via TRACE_EVENT(name : the
269 * 'subsystem_event' notation is fine.
271 * Think about this whole construct as the
272 * 'trace_sched_switch() function' from now on.
275 * TRACE_EVENT(sched_switch,
278 * * A function has a regular function arguments
279 * * prototype, declare it via TP_PROTO():
282 * TP_PROTO(struct rq *rq, struct task_struct *prev,
283 * struct task_struct *next),
286 * * Define the call signature of the 'function'.
287 * * (Design sidenote: we use this instead of a
288 * * TP_PROTO1/TP_PROTO2/TP_PROTO3 ugliness.)
291 * TP_ARGS(rq, prev, next),
294 * * Fast binary tracing: define the trace record via
295 * * TP_STRUCT__entry(). You can think about it like a
296 * * regular C structure local variable definition.
298 * * This is how the trace record is structured and will
299 * * be saved into the ring buffer. These are the fields
300 * * that will be exposed to user-space in
301 * * /sys/kernel/debug/tracing/events/<*>/format.
303 * * The declared 'local variable' is called '__entry'
305 * * __field(pid_t, prev_prid) is equivalent to a standard declariton:
309 * * __array(char, prev_comm, TASK_COMM_LEN) is equivalent to:
311 * * char prev_comm[TASK_COMM_LEN];
315 * __array( char, prev_comm, TASK_COMM_LEN )
316 * __field( pid_t, prev_pid )
317 * __field( int, prev_prio )
318 * __array( char, next_comm, TASK_COMM_LEN )
319 * __field( pid_t, next_pid )
320 * __field( int, next_prio )
324 * * Assign the entry into the trace record, by embedding
325 * * a full C statement block into TP_fast_assign(). You
326 * * can refer to the trace record as '__entry' -
327 * * otherwise you can put arbitrary C code in here.
329 * * Note: this C code will execute every time a trace event
330 * * happens, on an active tracepoint.
334 * memcpy(__entry->next_comm, next->comm, TASK_COMM_LEN);
335 * __entry->prev_pid = prev->pid;
336 * __entry->prev_prio = prev->prio;
337 * memcpy(__entry->prev_comm, prev->comm, TASK_COMM_LEN);
338 * __entry->next_pid = next->pid;
339 * __entry->next_prio = next->prio;
343 * * Formatted output of a trace record via TP_printk().
344 * * This is how the tracepoint will appear under ftrace
345 * * plugins that make use of this tracepoint.
347 * * (raw-binary tracing wont actually perform this step.)
350 * TP_printk("task %s:%d [%d] ==> %s:%d [%d]",
351 * __entry->prev_comm, __entry->prev_pid, __entry->prev_prio,
352 * __entry->next_comm, __entry->next_pid, __entry->next_prio),
356 * This macro construct is thus used for the regular printk format
357 * tracing setup, it is used to construct a function pointer based
358 * tracepoint callback (this is used by programmatic plugins and
359 * can also by used by generic instrumentation like SystemTap), and
360 * it is also used to expose a structured trace record in
361 * /sys/kernel/debug/tracing/events/.
363 * A set of (un)registration functions can be passed to the variant
364 * TRACE_EVENT_FN to perform any (un)registration work.
367 #define DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, print)
368 #define DEFINE_EVENT(template, name, proto, args) \
369 DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
370 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
371 DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
372 #define DEFINE_EVENT_CONDITION(template, name, proto, \
374 DECLARE_TRACE_CONDITION(name, PARAMS(proto), \
375 PARAMS(args), PARAMS(cond))
377 #define TRACE_EVENT(name, proto, args, struct, assign, print) \
378 DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
379 #define TRACE_EVENT_FN(name, proto, args, struct, \
380 assign, print, reg, unreg) \
381 DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
382 #define TRACE_EVENT_CONDITION(name, proto, args, cond, \
383 struct, assign, print) \
384 DECLARE_TRACE_CONDITION(name, PARAMS(proto), \
385 PARAMS(args), PARAMS(cond))
387 #define TRACE_EVENT_FLAGS(event, flag)
389 #endif /* ifdef TRACE_EVENT (see note above) */