]> git.karo-electronics.de Git - linux-beck.git/blob - include/linux/tracepoint.h
tracepoint: Use struct pointer instead of name hash for reg/unreg tracepoints
[linux-beck.git] / include / linux / tracepoint.h
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/errno.h>
18 #include <linux/types.h>
19 #include <linux/rcupdate.h>
20 #include <linux/static_key.h>
21
22 struct module;
23 struct tracepoint;
24 struct notifier_block;
25
26 struct tracepoint_func {
27         void *func;
28         void *data;
29 };
30
31 struct tracepoint {
32         const char *name;               /* Tracepoint name */
33         struct static_key key;
34         void (*regfunc)(void);
35         void (*unregfunc)(void);
36         struct tracepoint_func __rcu *funcs;
37 };
38
39 extern int
40 tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data);
41 extern int
42 tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data);
43 extern void
44 for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv),
45                 void *priv);
46
47 #ifdef CONFIG_MODULES
48 struct tp_module {
49         struct list_head list;
50         unsigned int num_tracepoints;
51         struct tracepoint * const *tracepoints_ptrs;
52 };
53
54 bool trace_module_has_bad_taint(struct module *mod);
55 extern int register_tracepoint_module_notifier(struct notifier_block *nb);
56 extern int unregister_tracepoint_module_notifier(struct notifier_block *nb);
57 #else
58 static inline bool trace_module_has_bad_taint(struct module *mod)
59 {
60         return false;
61 }
62 static inline
63 int register_tracepoint_module_notifier(struct notifier_block *nb)
64 {
65         return 0;
66 }
67 static inline
68 int unregister_tracepoint_module_notifier(struct notifier_block *nb)
69 {
70         return 0;
71 }
72 #endif /* CONFIG_MODULES */
73
74 /*
75  * tracepoint_synchronize_unregister must be called between the last tracepoint
76  * probe unregistration and the end of module exit to make sure there is no
77  * caller executing a probe when it is freed.
78  */
79 static inline void tracepoint_synchronize_unregister(void)
80 {
81         synchronize_sched();
82 }
83
84 #define PARAMS(args...) args
85
86 #endif /* _LINUX_TRACEPOINT_H */
87
88 /*
89  * Note: we keep the TRACE_EVENT and DECLARE_TRACE outside the include
90  *  file ifdef protection.
91  *  This is due to the way trace events work. If a file includes two
92  *  trace event headers under one "CREATE_TRACE_POINTS" the first include
93  *  will override the TRACE_EVENT and break the second include.
94  */
95
96 #ifndef DECLARE_TRACE
97
98 #define TP_PROTO(args...)       args
99 #define TP_ARGS(args...)        args
100 #define TP_CONDITION(args...)   args
101
102 #ifdef CONFIG_TRACEPOINTS
103
104 /*
105  * it_func[0] is never NULL because there is at least one element in the array
106  * when the array itself is non NULL.
107  *
108  * Note, the proto and args passed in includes "__data" as the first parameter.
109  * The reason for this is to handle the "void" prototype. If a tracepoint
110  * has a "void" prototype, then it is invalid to declare a function
111  * as "(void *, void)". The DECLARE_TRACE_NOARGS() will pass in just
112  * "void *data", where as the DECLARE_TRACE() will pass in "void *data, proto".
113  */
114 #define __DO_TRACE(tp, proto, args, cond, prercu, postrcu)              \
115         do {                                                            \
116                 struct tracepoint_func *it_func_ptr;                    \
117                 void *it_func;                                          \
118                 void *__data;                                           \
119                                                                         \
120                 if (!(cond))                                            \
121                         return;                                         \
122                 prercu;                                                 \
123                 rcu_read_lock_sched_notrace();                          \
124                 it_func_ptr = rcu_dereference_sched((tp)->funcs);       \
125                 if (it_func_ptr) {                                      \
126                         do {                                            \
127                                 it_func = (it_func_ptr)->func;          \
128                                 __data = (it_func_ptr)->data;           \
129                                 ((void(*)(proto))(it_func))(args);      \
130                         } while ((++it_func_ptr)->func);                \
131                 }                                                       \
132                 rcu_read_unlock_sched_notrace();                        \
133                 postrcu;                                                \
134         } while (0)
135
136 #ifndef MODULE
137 #define __DECLARE_TRACE_RCU(name, proto, args, cond, data_proto, data_args)     \
138         static inline void trace_##name##_rcuidle(proto)                \
139         {                                                               \
140                 if (static_key_false(&__tracepoint_##name.key))         \
141                         __DO_TRACE(&__tracepoint_##name,                \
142                                 TP_PROTO(data_proto),                   \
143                                 TP_ARGS(data_args),                     \
144                                 TP_CONDITION(cond),                     \
145                                 rcu_irq_enter(),                        \
146                                 rcu_irq_exit());                        \
147         }
148 #else
149 #define __DECLARE_TRACE_RCU(name, proto, args, cond, data_proto, data_args)
150 #endif
151
152 /*
153  * Make sure the alignment of the structure in the __tracepoints section will
154  * not add unwanted padding between the beginning of the section and the
155  * structure. Force alignment to the same alignment as the section start.
156  */
157 #define __DECLARE_TRACE(name, proto, args, cond, data_proto, data_args) \
158         extern struct tracepoint __tracepoint_##name;                   \
159         static inline void trace_##name(proto)                          \
160         {                                                               \
161                 if (static_key_false(&__tracepoint_##name.key))         \
162                         __DO_TRACE(&__tracepoint_##name,                \
163                                 TP_PROTO(data_proto),                   \
164                                 TP_ARGS(data_args),                     \
165                                 TP_CONDITION(cond),,);                  \
166         }                                                               \
167         __DECLARE_TRACE_RCU(name, PARAMS(proto), PARAMS(args),          \
168                 PARAMS(cond), PARAMS(data_proto), PARAMS(data_args))    \
169         static inline int                                               \
170         register_trace_##name(void (*probe)(data_proto), void *data)    \
171         {                                                               \
172                 return tracepoint_probe_register(&__tracepoint_##name,  \
173                                                 (void *)probe, data);   \
174         }                                                               \
175         static inline int                                               \
176         unregister_trace_##name(void (*probe)(data_proto), void *data)  \
177         {                                                               \
178                 return tracepoint_probe_unregister(&__tracepoint_##name,\
179                                                 (void *)probe, data);   \
180         }                                                               \
181         static inline void                                              \
182         check_trace_callback_type_##name(void (*cb)(data_proto))        \
183         {                                                               \
184         }
185
186 /*
187  * We have no guarantee that gcc and the linker won't up-align the tracepoint
188  * structures, so we create an array of pointers that will be used for iteration
189  * on the tracepoints.
190  */
191 #define DEFINE_TRACE_FN(name, reg, unreg)                                \
192         static const char __tpstrtab_##name[]                            \
193         __attribute__((section("__tracepoints_strings"))) = #name;       \
194         struct tracepoint __tracepoint_##name                            \
195         __attribute__((section("__tracepoints"))) =                      \
196                 { __tpstrtab_##name, STATIC_KEY_INIT_FALSE, reg, unreg, NULL };\
197         static struct tracepoint * const __tracepoint_ptr_##name __used  \
198         __attribute__((section("__tracepoints_ptrs"))) =                 \
199                 &__tracepoint_##name;
200
201 #define DEFINE_TRACE(name)                                              \
202         DEFINE_TRACE_FN(name, NULL, NULL);
203
204 #define EXPORT_TRACEPOINT_SYMBOL_GPL(name)                              \
205         EXPORT_SYMBOL_GPL(__tracepoint_##name)
206 #define EXPORT_TRACEPOINT_SYMBOL(name)                                  \
207         EXPORT_SYMBOL(__tracepoint_##name)
208
209 #else /* !CONFIG_TRACEPOINTS */
210 #define __DECLARE_TRACE(name, proto, args, cond, data_proto, data_args) \
211         static inline void trace_##name(proto)                          \
212         { }                                                             \
213         static inline void trace_##name##_rcuidle(proto)                \
214         { }                                                             \
215         static inline int                                               \
216         register_trace_##name(void (*probe)(data_proto),                \
217                               void *data)                               \
218         {                                                               \
219                 return -ENOSYS;                                         \
220         }                                                               \
221         static inline int                                               \
222         unregister_trace_##name(void (*probe)(data_proto),              \
223                                 void *data)                             \
224         {                                                               \
225                 return -ENOSYS;                                         \
226         }                                                               \
227         static inline void check_trace_callback_type_##name(void (*cb)(data_proto)) \
228         {                                                               \
229         }
230
231 #define DEFINE_TRACE_FN(name, reg, unreg)
232 #define DEFINE_TRACE(name)
233 #define EXPORT_TRACEPOINT_SYMBOL_GPL(name)
234 #define EXPORT_TRACEPOINT_SYMBOL(name)
235
236 #endif /* CONFIG_TRACEPOINTS */
237
238 /*
239  * The need for the DECLARE_TRACE_NOARGS() is to handle the prototype
240  * (void). "void" is a special value in a function prototype and can
241  * not be combined with other arguments. Since the DECLARE_TRACE()
242  * macro adds a data element at the beginning of the prototype,
243  * we need a way to differentiate "(void *data, proto)" from
244  * "(void *data, void)". The second prototype is invalid.
245  *
246  * DECLARE_TRACE_NOARGS() passes "void" as the tracepoint prototype
247  * and "void *__data" as the callback prototype.
248  *
249  * DECLARE_TRACE() passes "proto" as the tracepoint protoype and
250  * "void *__data, proto" as the callback prototype.
251  */
252 #define DECLARE_TRACE_NOARGS(name)                                      \
253                 __DECLARE_TRACE(name, void, , 1, void *__data, __data)
254
255 #define DECLARE_TRACE(name, proto, args)                                \
256                 __DECLARE_TRACE(name, PARAMS(proto), PARAMS(args), 1,   \
257                                 PARAMS(void *__data, proto),            \
258                                 PARAMS(__data, args))
259
260 #define DECLARE_TRACE_CONDITION(name, proto, args, cond)                \
261         __DECLARE_TRACE(name, PARAMS(proto), PARAMS(args), PARAMS(cond), \
262                         PARAMS(void *__data, proto),                    \
263                         PARAMS(__data, args))
264
265 #define TRACE_EVENT_FLAGS(event, flag)
266
267 #define TRACE_EVENT_PERF_PERM(event, expr...)
268
269 #endif /* DECLARE_TRACE */
270
271 #ifndef TRACE_EVENT
272 /*
273  * For use with the TRACE_EVENT macro:
274  *
275  * We define a tracepoint, its arguments, its printk format
276  * and its 'fast binary record' layout.
277  *
278  * Firstly, name your tracepoint via TRACE_EVENT(name : the
279  * 'subsystem_event' notation is fine.
280  *
281  * Think about this whole construct as the
282  * 'trace_sched_switch() function' from now on.
283  *
284  *
285  *  TRACE_EVENT(sched_switch,
286  *
287  *      *
288  *      * A function has a regular function arguments
289  *      * prototype, declare it via TP_PROTO():
290  *      *
291  *
292  *      TP_PROTO(struct rq *rq, struct task_struct *prev,
293  *               struct task_struct *next),
294  *
295  *      *
296  *      * Define the call signature of the 'function'.
297  *      * (Design sidenote: we use this instead of a
298  *      *  TP_PROTO1/TP_PROTO2/TP_PROTO3 ugliness.)
299  *      *
300  *
301  *      TP_ARGS(rq, prev, next),
302  *
303  *      *
304  *      * Fast binary tracing: define the trace record via
305  *      * TP_STRUCT__entry(). You can think about it like a
306  *      * regular C structure local variable definition.
307  *      *
308  *      * This is how the trace record is structured and will
309  *      * be saved into the ring buffer. These are the fields
310  *      * that will be exposed to user-space in
311  *      * /sys/kernel/debug/tracing/events/<*>/format.
312  *      *
313  *      * The declared 'local variable' is called '__entry'
314  *      *
315  *      * __field(pid_t, prev_prid) is equivalent to a standard declariton:
316  *      *
317  *      *       pid_t   prev_pid;
318  *      *
319  *      * __array(char, prev_comm, TASK_COMM_LEN) is equivalent to:
320  *      *
321  *      *       char    prev_comm[TASK_COMM_LEN];
322  *      *
323  *
324  *      TP_STRUCT__entry(
325  *              __array(        char,   prev_comm,      TASK_COMM_LEN   )
326  *              __field(        pid_t,  prev_pid                        )
327  *              __field(        int,    prev_prio                       )
328  *              __array(        char,   next_comm,      TASK_COMM_LEN   )
329  *              __field(        pid_t,  next_pid                        )
330  *              __field(        int,    next_prio                       )
331  *      ),
332  *
333  *      *
334  *      * Assign the entry into the trace record, by embedding
335  *      * a full C statement block into TP_fast_assign(). You
336  *      * can refer to the trace record as '__entry' -
337  *      * otherwise you can put arbitrary C code in here.
338  *      *
339  *      * Note: this C code will execute every time a trace event
340  *      * happens, on an active tracepoint.
341  *      *
342  *
343  *      TP_fast_assign(
344  *              memcpy(__entry->next_comm, next->comm, TASK_COMM_LEN);
345  *              __entry->prev_pid       = prev->pid;
346  *              __entry->prev_prio      = prev->prio;
347  *              memcpy(__entry->prev_comm, prev->comm, TASK_COMM_LEN);
348  *              __entry->next_pid       = next->pid;
349  *              __entry->next_prio      = next->prio;
350  *      ),
351  *
352  *      *
353  *      * Formatted output of a trace record via TP_printk().
354  *      * This is how the tracepoint will appear under ftrace
355  *      * plugins that make use of this tracepoint.
356  *      *
357  *      * (raw-binary tracing wont actually perform this step.)
358  *      *
359  *
360  *      TP_printk("task %s:%d [%d] ==> %s:%d [%d]",
361  *              __entry->prev_comm, __entry->prev_pid, __entry->prev_prio,
362  *              __entry->next_comm, __entry->next_pid, __entry->next_prio),
363  *
364  * );
365  *
366  * This macro construct is thus used for the regular printk format
367  * tracing setup, it is used to construct a function pointer based
368  * tracepoint callback (this is used by programmatic plugins and
369  * can also by used by generic instrumentation like SystemTap), and
370  * it is also used to expose a structured trace record in
371  * /sys/kernel/debug/tracing/events/.
372  *
373  * A set of (un)registration functions can be passed to the variant
374  * TRACE_EVENT_FN to perform any (un)registration work.
375  */
376
377 #define DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, print)
378 #define DEFINE_EVENT(template, name, proto, args)               \
379         DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
380 #define DEFINE_EVENT_FN(template, name, proto, args, reg, unreg)\
381         DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
382 #define DEFINE_EVENT_PRINT(template, name, proto, args, print)  \
383         DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
384 #define DEFINE_EVENT_CONDITION(template, name, proto,           \
385                                args, cond)                      \
386         DECLARE_TRACE_CONDITION(name, PARAMS(proto),            \
387                                 PARAMS(args), PARAMS(cond))
388
389 #define TRACE_EVENT(name, proto, args, struct, assign, print)   \
390         DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
391 #define TRACE_EVENT_FN(name, proto, args, struct,               \
392                 assign, print, reg, unreg)                      \
393         DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
394 #define TRACE_EVENT_CONDITION(name, proto, args, cond,          \
395                               struct, assign, print)            \
396         DECLARE_TRACE_CONDITION(name, PARAMS(proto),            \
397                                 PARAMS(args), PARAMS(cond))
398
399 #define TRACE_EVENT_FLAGS(event, flag)
400
401 #define TRACE_EVENT_PERF_PERM(event, expr...)
402
403 #endif /* ifdef TRACE_EVENT (see note above) */