8d5e4f6d96d0596c47ba2c90a844e1a3ff02c158
[linux-2.6.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  * (C) Copyright 2008 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
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         static inline void check_trace_callback_type_##name(void (*cb)(proto)) \
142         {                                                               \
143         }
144
145 #define DEFINE_TRACE_FN(name, reg, unreg)                               \
146         static const char __tpstrtab_##name[]                           \
147         __attribute__((section("__tracepoints_strings"))) = #name;      \
148         struct tracepoint __tracepoint_##name                           \
149         __attribute__((section("__tracepoints"), aligned(32))) =        \
150                 { __tpstrtab_##name, 0, reg, unreg, NULL }
151
152 #define DEFINE_TRACE(name)                                              \
153         DEFINE_TRACE_FN(name, NULL, NULL);
154
155 #define EXPORT_TRACEPOINT_SYMBOL_GPL(name)                              \
156         EXPORT_SYMBOL_GPL(__tracepoint_##name)
157 #define EXPORT_TRACEPOINT_SYMBOL(name)                                  \
158         EXPORT_SYMBOL(__tracepoint_##name)
159
160 #else /* !CONFIG_TRACEPOINTS */
161 #define DECLARE_TRACE(name, proto, args)                                \
162         static inline void _do_trace_##name(struct tracepoint *tp, proto) \
163         { }                                                             \
164         static inline void trace_##name(proto)                          \
165         { }                                                             \
166         static inline int register_trace_##name(void (*probe)(proto))   \
167         {                                                               \
168                 return -ENOSYS;                                         \
169         }                                                               \
170         static inline int unregister_trace_##name(void (*probe)(proto)) \
171         {                                                               \
172                 return -ENOSYS;                                         \
173         }                                                               \
174         static inline void check_trace_callback_type_##name(void (*cb)(proto)) \
175         {                                                               \
176         }
177
178 #define DEFINE_TRACE_FN(name, reg, unreg)
179 #define DEFINE_TRACE(name)
180 #define EXPORT_TRACEPOINT_SYMBOL_GPL(name)
181 #define EXPORT_TRACEPOINT_SYMBOL(name)
182
183 #endif /* CONFIG_TRACEPOINTS */
184 #endif /* DECLARE_TRACE */
185
186 #ifndef TRACE_EVENT
187 /*
188  * For use with the TRACE_EVENT macro:
189  *
190  * We define a tracepoint, its arguments, its printk format
191  * and its 'fast binay record' layout.
192  *
193  * Firstly, name your tracepoint via TRACE_EVENT(name : the
194  * 'subsystem_event' notation is fine.
195  *
196  * Think about this whole construct as the
197  * 'trace_sched_switch() function' from now on.
198  *
199  *
200  *  TRACE_EVENT(sched_switch,
201  *
202  *      *
203  *      * A function has a regular function arguments
204  *      * prototype, declare it via TP_PROTO():
205  *      *
206  *
207  *      TP_PROTO(struct rq *rq, struct task_struct *prev,
208  *               struct task_struct *next),
209  *
210  *      *
211  *      * Define the call signature of the 'function'.
212  *      * (Design sidenote: we use this instead of a
213  *      *  TP_PROTO1/TP_PROTO2/TP_PROTO3 ugliness.)
214  *      *
215  *
216  *      TP_ARGS(rq, prev, next),
217  *
218  *      *
219  *      * Fast binary tracing: define the trace record via
220  *      * TP_STRUCT__entry(). You can think about it like a
221  *      * regular C structure local variable definition.
222  *      *
223  *      * This is how the trace record is structured and will
224  *      * be saved into the ring buffer. These are the fields
225  *      * that will be exposed to user-space in
226  *      * /sys/kernel/debug/tracing/events/<*>/format.
227  *      *
228  *      * The declared 'local variable' is called '__entry'
229  *      *
230  *      * __field(pid_t, prev_prid) is equivalent to a standard declariton:
231  *      *
232  *      *       pid_t   prev_pid;
233  *      *
234  *      * __array(char, prev_comm, TASK_COMM_LEN) is equivalent to:
235  *      *
236  *      *       char    prev_comm[TASK_COMM_LEN];
237  *      *
238  *
239  *      TP_STRUCT__entry(
240  *              __array(        char,   prev_comm,      TASK_COMM_LEN   )
241  *              __field(        pid_t,  prev_pid                        )
242  *              __field(        int,    prev_prio                       )
243  *              __array(        char,   next_comm,      TASK_COMM_LEN   )
244  *              __field(        pid_t,  next_pid                        )
245  *              __field(        int,    next_prio                       )
246  *      ),
247  *
248  *      *
249  *      * Assign the entry into the trace record, by embedding
250  *      * a full C statement block into TP_fast_assign(). You
251  *      * can refer to the trace record as '__entry' -
252  *      * otherwise you can put arbitrary C code in here.
253  *      *
254  *      * Note: this C code will execute every time a trace event
255  *      * happens, on an active tracepoint.
256  *      *
257  *
258  *      TP_fast_assign(
259  *              memcpy(__entry->next_comm, next->comm, TASK_COMM_LEN);
260  *              __entry->prev_pid       = prev->pid;
261  *              __entry->prev_prio      = prev->prio;
262  *              memcpy(__entry->prev_comm, prev->comm, TASK_COMM_LEN);
263  *              __entry->next_pid       = next->pid;
264  *              __entry->next_prio      = next->prio;
265  *      )
266  *
267  *      *
268  *      * Formatted output of a trace record via TP_printk().
269  *      * This is how the tracepoint will appear under ftrace
270  *      * plugins that make use of this tracepoint.
271  *      *
272  *      * (raw-binary tracing wont actually perform this step.)
273  *      *
274  *
275  *      TP_printk("task %s:%d [%d] ==> %s:%d [%d]",
276  *              __entry->prev_comm, __entry->prev_pid, __entry->prev_prio,
277  *              __entry->next_comm, __entry->next_pid, __entry->next_prio),
278  *
279  * );
280  *
281  * This macro construct is thus used for the regular printk format
282  * tracing setup, it is used to construct a function pointer based
283  * tracepoint callback (this is used by programmatic plugins and
284  * can also by used by generic instrumentation like SystemTap), and
285  * it is also used to expose a structured trace record in
286  * /sys/kernel/debug/tracing/events/.
287  *
288  * A set of (un)registration functions can be passed to the variant
289  * TRACE_EVENT_FN to perform any (un)registration work.
290  */
291
292 #define DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, print)
293 #define DEFINE_EVENT(template, name, proto, args)               \
294         DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
295 #define DEFINE_EVENT_PRINT(template, name, proto, args, print)  \
296         DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
297
298 #define TRACE_EVENT(name, proto, args, struct, assign, print)   \
299         DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
300 #define TRACE_EVENT_FN(name, proto, args, struct,               \
301                 assign, print, reg, unreg)                      \
302         DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
303
304 #endif /* ifdef TRACE_EVENT (see note above) */