blob: 0341f2e2698ade32754e77c0717611918d2de044 [file] [log] [blame]
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04001#ifndef _LINUX_TRACEPOINT_H
2#define _LINUX_TRACEPOINT_H
3
4/*
5 * Kernel Tracepoint API.
6 *
7 * See Documentation/tracepoint.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
20struct module;
21struct tracepoint;
22
23struct tracepoint {
24 const char *name; /* Tracepoint name */
25 int state; /* State. */
26 void **funcs;
Mathieu Desnoyers7e066fb2008-11-14 17:47:47 -050027} __attribute__((aligned(32))); /*
28 * Aligned on 32 bytes because it is
29 * globally visible and gcc happily
30 * align these on the structure size.
31 * Keep in sync with vmlinux.lds.h.
32 */
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040033
Steven Rostedtea20d922009-04-10 08:54:16 -040034#ifndef DECLARE_TRACE
35
Steven Rostedt2939b042009-03-09 15:47:18 -040036#define TP_PROTO(args...) args
37#define TP_ARGS(args...) args
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040038
39#ifdef CONFIG_TRACEPOINTS
40
41/*
42 * it_func[0] is never NULL because there is at least one element in the array
43 * when the array itself is non NULL.
44 */
45#define __DO_TRACE(tp, proto, args) \
46 do { \
47 void **it_func; \
48 \
Mathieu Desnoyersda7b3ea2008-11-14 17:47:43 -050049 rcu_read_lock_sched_notrace(); \
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040050 it_func = rcu_dereference((tp)->funcs); \
51 if (it_func) { \
52 do { \
53 ((void(*)(proto))(*it_func))(args); \
54 } while (*(++it_func)); \
55 } \
Mathieu Desnoyersda7b3ea2008-11-14 17:47:43 -050056 rcu_read_unlock_sched_notrace(); \
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040057 } while (0)
58
59/*
60 * Make sure the alignment of the structure in the __tracepoints section will
61 * not add unwanted padding between the beginning of the section and the
62 * structure. Force alignment to the same alignment as the section start.
Jason Baron63fbdab2009-08-10 16:52:27 -040063 * An optional set of (un)registration functions can be passed to perform any
64 * additional (un)registration work.
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040065 */
Jason Baron63fbdab2009-08-10 16:52:27 -040066#define DECLARE_TRACE_WITH_CALLBACK(name, proto, args, reg, unreg) \
Mathieu Desnoyers7e066fb2008-11-14 17:47:47 -050067 extern struct tracepoint __tracepoint_##name; \
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040068 static inline void trace_##name(proto) \
69 { \
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040070 if (unlikely(__tracepoint_##name.state)) \
71 __DO_TRACE(&__tracepoint_##name, \
Steven Rostedt2939b042009-03-09 15:47:18 -040072 TP_PROTO(proto), TP_ARGS(args)); \
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040073 } \
74 static inline int register_trace_##name(void (*probe)(proto)) \
75 { \
Jason Baron63fbdab2009-08-10 16:52:27 -040076 int ret; \
77 void (*func)(void) = reg; \
78 \
79 ret = tracepoint_probe_register(#name, (void *)probe); \
80 if (func && !ret) \
81 func(); \
82 return ret; \
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040083 } \
Mathieu Desnoyersc4209702008-11-14 17:47:44 -050084 static inline int unregister_trace_##name(void (*probe)(proto)) \
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040085 { \
Jason Baron63fbdab2009-08-10 16:52:27 -040086 int ret; \
87 void (*func)(void) = unreg; \
88 \
89 ret = tracepoint_probe_unregister(#name, (void *)probe);\
90 if (func && !ret) \
91 func(); \
92 return ret; \
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040093 }
94
Jason Baron63fbdab2009-08-10 16:52:27 -040095
96#define DECLARE_TRACE(name, proto, args) \
97 DECLARE_TRACE_WITH_CALLBACK(name, TP_PROTO(proto), TP_ARGS(args),\
98 NULL, NULL);
99
Mathieu Desnoyers7e066fb2008-11-14 17:47:47 -0500100#define DEFINE_TRACE(name) \
101 static const char __tpstrtab_##name[] \
102 __attribute__((section("__tracepoints_strings"))) = #name; \
103 struct tracepoint __tracepoint_##name \
104 __attribute__((section("__tracepoints"), aligned(32))) = \
105 { __tpstrtab_##name, 0, NULL }
106
107#define EXPORT_TRACEPOINT_SYMBOL_GPL(name) \
108 EXPORT_SYMBOL_GPL(__tracepoint_##name)
109#define EXPORT_TRACEPOINT_SYMBOL(name) \
110 EXPORT_SYMBOL(__tracepoint_##name)
111
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400112extern void tracepoint_update_probe_range(struct tracepoint *begin,
113 struct tracepoint *end);
114
115#else /* !CONFIG_TRACEPOINTS */
Jason Baron63fbdab2009-08-10 16:52:27 -0400116#define DECLARE_TRACE_WITH_CALLBACK(name, proto, args, reg, unreg) \
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400117 static inline void _do_trace_##name(struct tracepoint *tp, proto) \
118 { } \
119 static inline void trace_##name(proto) \
120 { } \
121 static inline int register_trace_##name(void (*probe)(proto)) \
122 { \
123 return -ENOSYS; \
124 } \
Mathieu Desnoyersc4209702008-11-14 17:47:44 -0500125 static inline int unregister_trace_##name(void (*probe)(proto)) \
126 { \
127 return -ENOSYS; \
128 }
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400129
Jason Baron63fbdab2009-08-10 16:52:27 -0400130#define DECLARE_TRACE(name, proto, args) \
131 DECLARE_TRACE_WITH_CALLBACK(name, TP_PROTO(proto), TP_ARGS(args),\
132 NULL, NULL);
133
Mathieu Desnoyers7e066fb2008-11-14 17:47:47 -0500134#define DEFINE_TRACE(name)
135#define EXPORT_TRACEPOINT_SYMBOL_GPL(name)
136#define EXPORT_TRACEPOINT_SYMBOL(name)
137
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400138static inline void tracepoint_update_probe_range(struct tracepoint *begin,
139 struct tracepoint *end)
140{ }
141#endif /* CONFIG_TRACEPOINTS */
Steven Rostedtea20d922009-04-10 08:54:16 -0400142#endif /* DECLARE_TRACE */
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400143
144/*
145 * Connect a probe to a tracepoint.
146 * Internal API, should not be used directly.
147 */
148extern int tracepoint_probe_register(const char *name, void *probe);
149
150/*
151 * Disconnect a probe from a tracepoint.
152 * Internal API, should not be used directly.
153 */
154extern int tracepoint_probe_unregister(const char *name, void *probe);
155
Lai Jiangshan127cafb2008-10-28 10:51:53 +0800156extern int tracepoint_probe_register_noupdate(const char *name, void *probe);
157extern int tracepoint_probe_unregister_noupdate(const char *name, void *probe);
158extern void tracepoint_probe_update_all(void);
159
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400160struct tracepoint_iter {
161 struct module *module;
162 struct tracepoint *tracepoint;
163};
164
165extern void tracepoint_iter_start(struct tracepoint_iter *iter);
166extern void tracepoint_iter_next(struct tracepoint_iter *iter);
167extern void tracepoint_iter_stop(struct tracepoint_iter *iter);
168extern void tracepoint_iter_reset(struct tracepoint_iter *iter);
169extern int tracepoint_get_iter_range(struct tracepoint **tracepoint,
170 struct tracepoint *begin, struct tracepoint *end);
171
Mathieu Desnoyersf2461fc2008-10-06 10:33:00 -0400172/*
173 * tracepoint_synchronize_unregister must be called between the last tracepoint
174 * probe unregistration and the end of module exit to make sure there is no
175 * caller executing a probe when it is freed.
176 */
Mathieu Desnoyers231375c2008-10-03 15:01:33 -0400177static inline void tracepoint_synchronize_unregister(void)
178{
179 synchronize_sched();
180}
Mathieu Desnoyersf2461fc2008-10-06 10:33:00 -0400181
Steven Rostedt3cdfdf92009-02-25 15:54:30 -0500182#define PARAMS(args...) args
Steven Rostedt7cb2e3e2009-08-26 00:32:37 -0400183
184#endif /* _LINUX_TRACEPOINT_H */
185
186/*
187 * Note: we keep the TRACE_EVENT outside the include file ifdef protection.
188 * This is due to the way trace events work. If a file includes two
189 * trace event headers under one "CREATE_TRACE_POINTS" the first include
190 * will override the TRACE_EVENT and break the second include.
191 */
Steven Rostedtea20d922009-04-10 08:54:16 -0400192
Steven Rostedtea20d922009-04-10 08:54:16 -0400193#ifndef TRACE_EVENT
Steven Rostedt823f9122009-03-10 12:58:51 -0400194/*
195 * For use with the TRACE_EVENT macro:
196 *
197 * We define a tracepoint, its arguments, its printk format
198 * and its 'fast binay record' layout.
199 *
200 * Firstly, name your tracepoint via TRACE_EVENT(name : the
201 * 'subsystem_event' notation is fine.
202 *
203 * Think about this whole construct as the
204 * 'trace_sched_switch() function' from now on.
205 *
206 *
207 * TRACE_EVENT(sched_switch,
208 *
209 * *
210 * * A function has a regular function arguments
211 * * prototype, declare it via TP_PROTO():
212 * *
213 *
Steven Rostedtef180122009-03-10 14:10:56 -0400214 * TP_PROTO(struct rq *rq, struct task_struct *prev,
215 * struct task_struct *next),
Steven Rostedt823f9122009-03-10 12:58:51 -0400216 *
217 * *
218 * * Define the call signature of the 'function'.
219 * * (Design sidenote: we use this instead of a
220 * * TP_PROTO1/TP_PROTO2/TP_PROTO3 ugliness.)
221 * *
222 *
Steven Rostedtef180122009-03-10 14:10:56 -0400223 * TP_ARGS(rq, prev, next),
Steven Rostedt823f9122009-03-10 12:58:51 -0400224 *
225 * *
226 * * Fast binary tracing: define the trace record via
227 * * TP_STRUCT__entry(). You can think about it like a
228 * * regular C structure local variable definition.
229 * *
230 * * This is how the trace record is structured and will
231 * * be saved into the ring buffer. These are the fields
232 * * that will be exposed to user-space in
GeunSik Lim156f5a72009-06-02 15:01:37 +0900233 * * /sys/kernel/debug/tracing/events/<*>/format.
Steven Rostedt823f9122009-03-10 12:58:51 -0400234 * *
235 * * The declared 'local variable' is called '__entry'
236 * *
237 * * __field(pid_t, prev_prid) is equivalent to a standard declariton:
238 * *
239 * * pid_t prev_pid;
240 * *
241 * * __array(char, prev_comm, TASK_COMM_LEN) is equivalent to:
242 * *
243 * * char prev_comm[TASK_COMM_LEN];
244 * *
245 *
246 * TP_STRUCT__entry(
247 * __array( char, prev_comm, TASK_COMM_LEN )
248 * __field( pid_t, prev_pid )
249 * __field( int, prev_prio )
250 * __array( char, next_comm, TASK_COMM_LEN )
251 * __field( pid_t, next_pid )
252 * __field( int, next_prio )
253 * ),
254 *
255 * *
256 * * Assign the entry into the trace record, by embedding
257 * * a full C statement block into TP_fast_assign(). You
258 * * can refer to the trace record as '__entry' -
259 * * otherwise you can put arbitrary C code in here.
260 * *
261 * * Note: this C code will execute every time a trace event
262 * * happens, on an active tracepoint.
263 * *
264 *
Steven Rostedtef180122009-03-10 14:10:56 -0400265 * TP_fast_assign(
266 * memcpy(__entry->next_comm, next->comm, TASK_COMM_LEN);
267 * __entry->prev_pid = prev->pid;
268 * __entry->prev_prio = prev->prio;
Steven Rostedt823f9122009-03-10 12:58:51 -0400269 * memcpy(__entry->prev_comm, prev->comm, TASK_COMM_LEN);
270 * __entry->next_pid = next->pid;
Steven Rostedtef180122009-03-10 14:10:56 -0400271 * __entry->next_prio = next->prio;
Steven Rostedt823f9122009-03-10 12:58:51 -0400272 * )
273 *
274 * *
275 * * Formatted output of a trace record via TP_printk().
276 * * This is how the tracepoint will appear under ftrace
277 * * plugins that make use of this tracepoint.
278 * *
279 * * (raw-binary tracing wont actually perform this step.)
280 * *
281 *
282 * TP_printk("task %s:%d [%d] ==> %s:%d [%d]",
283 * __entry->prev_comm, __entry->prev_pid, __entry->prev_prio,
284 * __entry->next_comm, __entry->next_pid, __entry->next_prio),
285 *
286 * );
287 *
288 * This macro construct is thus used for the regular printk format
289 * tracing setup, it is used to construct a function pointer based
290 * tracepoint callback (this is used by programmatic plugins and
291 * can also by used by generic instrumentation like SystemTap), and
292 * it is also used to expose a structured trace record in
GeunSik Lim156f5a72009-06-02 15:01:37 +0900293 * /sys/kernel/debug/tracing/events/.
Steven Rostedt823f9122009-03-10 12:58:51 -0400294 */
295
Steven Rostedt30a8fec2009-03-10 12:41:38 -0400296#define TRACE_EVENT(name, proto, args, struct, assign, print) \
Steven Rostedtda4d0302009-03-09 17:14:30 -0400297 DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
Steven Rostedt7cb2e3e2009-08-26 00:32:37 -0400298
299#endif /* ifdef TRACE_EVENT (see note above) */