blob: 846a4ae501eb8ba675da0b03274de9d26c7ee555 [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. */
Josh Stone97419872009-08-24 14:43:13 -070026 void (*regfunc)(void);
27 void (*unregfunc)(void);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040028 void **funcs;
Mathieu Desnoyers7e066fb2008-11-14 17:47:47 -050029} __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 */
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040035
Steven Rostedtea20d922009-04-10 08:54:16 -040036#ifndef DECLARE_TRACE
37
Steven Rostedt2939b042009-03-09 15:47:18 -040038#define TP_PROTO(args...) args
39#define TP_ARGS(args...) args
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040040
41#ifdef CONFIG_TRACEPOINTS
42
43/*
44 * it_func[0] is never NULL because there is at least one element in the array
45 * when the array itself is non NULL.
46 */
47#define __DO_TRACE(tp, proto, args) \
48 do { \
49 void **it_func; \
50 \
Mathieu Desnoyersda7b3ea2008-11-14 17:47:43 -050051 rcu_read_lock_sched_notrace(); \
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040052 it_func = rcu_dereference((tp)->funcs); \
53 if (it_func) { \
54 do { \
55 ((void(*)(proto))(*it_func))(args); \
56 } while (*(++it_func)); \
57 } \
Mathieu Desnoyersda7b3ea2008-11-14 17:47:43 -050058 rcu_read_unlock_sched_notrace(); \
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040059 } while (0)
60
61/*
62 * Make sure the alignment of the structure in the __tracepoints section will
63 * not add unwanted padding between the beginning of the section and the
64 * structure. Force alignment to the same alignment as the section start.
65 */
Josh Stone97419872009-08-24 14:43:13 -070066#define DECLARE_TRACE(name, proto, args) \
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 { \
Josh Stone97419872009-08-24 14:43:13 -070076 return tracepoint_probe_register(#name, (void *)probe); \
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040077 } \
Mathieu Desnoyersc4209702008-11-14 17:47:44 -050078 static inline int unregister_trace_##name(void (*probe)(proto)) \
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040079 { \
Josh Stone97419872009-08-24 14:43:13 -070080 return tracepoint_probe_unregister(#name, (void *)probe);\
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040081 }
82
Jason Baron63fbdab2009-08-10 16:52:27 -040083
Josh Stone97419872009-08-24 14:43:13 -070084#define DEFINE_TRACE_FN(name, reg, unreg) \
Mathieu Desnoyers7e066fb2008-11-14 17:47:47 -050085 static const char __tpstrtab_##name[] \
86 __attribute__((section("__tracepoints_strings"))) = #name; \
87 struct tracepoint __tracepoint_##name \
88 __attribute__((section("__tracepoints"), aligned(32))) = \
Josh Stone97419872009-08-24 14:43:13 -070089 { __tpstrtab_##name, 0, reg, unreg, NULL }
90
91#define DEFINE_TRACE(name) \
92 DEFINE_TRACE_FN(name, NULL, NULL);
Mathieu Desnoyers7e066fb2008-11-14 17:47:47 -050093
94#define EXPORT_TRACEPOINT_SYMBOL_GPL(name) \
95 EXPORT_SYMBOL_GPL(__tracepoint_##name)
96#define EXPORT_TRACEPOINT_SYMBOL(name) \
97 EXPORT_SYMBOL(__tracepoint_##name)
98
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040099extern void tracepoint_update_probe_range(struct tracepoint *begin,
100 struct tracepoint *end);
101
102#else /* !CONFIG_TRACEPOINTS */
Josh Stone97419872009-08-24 14:43:13 -0700103#define DECLARE_TRACE(name, proto, args) \
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400104 static inline void _do_trace_##name(struct tracepoint *tp, proto) \
105 { } \
106 static inline void trace_##name(proto) \
107 { } \
108 static inline int register_trace_##name(void (*probe)(proto)) \
109 { \
110 return -ENOSYS; \
111 } \
Mathieu Desnoyersc4209702008-11-14 17:47:44 -0500112 static inline int unregister_trace_##name(void (*probe)(proto)) \
113 { \
114 return -ENOSYS; \
115 }
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400116
Josh Stone97419872009-08-24 14:43:13 -0700117#define DEFINE_TRACE_FN(name, reg, unreg)
Mathieu Desnoyers7e066fb2008-11-14 17:47:47 -0500118#define DEFINE_TRACE(name)
119#define EXPORT_TRACEPOINT_SYMBOL_GPL(name)
120#define EXPORT_TRACEPOINT_SYMBOL(name)
121
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400122static inline void tracepoint_update_probe_range(struct tracepoint *begin,
123 struct tracepoint *end)
124{ }
125#endif /* CONFIG_TRACEPOINTS */
Steven Rostedtea20d922009-04-10 08:54:16 -0400126#endif /* DECLARE_TRACE */
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400127
128/*
129 * Connect a probe to a tracepoint.
130 * Internal API, should not be used directly.
131 */
132extern int tracepoint_probe_register(const char *name, void *probe);
133
134/*
135 * Disconnect a probe from a tracepoint.
136 * Internal API, should not be used directly.
137 */
138extern int tracepoint_probe_unregister(const char *name, void *probe);
139
Lai Jiangshan127cafb2008-10-28 10:51:53 +0800140extern int tracepoint_probe_register_noupdate(const char *name, void *probe);
141extern int tracepoint_probe_unregister_noupdate(const char *name, void *probe);
142extern void tracepoint_probe_update_all(void);
143
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400144struct tracepoint_iter {
145 struct module *module;
146 struct tracepoint *tracepoint;
147};
148
149extern void tracepoint_iter_start(struct tracepoint_iter *iter);
150extern void tracepoint_iter_next(struct tracepoint_iter *iter);
151extern void tracepoint_iter_stop(struct tracepoint_iter *iter);
152extern void tracepoint_iter_reset(struct tracepoint_iter *iter);
153extern int tracepoint_get_iter_range(struct tracepoint **tracepoint,
154 struct tracepoint *begin, struct tracepoint *end);
155
Mathieu Desnoyersf2461fc2008-10-06 10:33:00 -0400156/*
157 * tracepoint_synchronize_unregister must be called between the last tracepoint
158 * probe unregistration and the end of module exit to make sure there is no
159 * caller executing a probe when it is freed.
160 */
Mathieu Desnoyers231375c2008-10-03 15:01:33 -0400161static inline void tracepoint_synchronize_unregister(void)
162{
163 synchronize_sched();
164}
Mathieu Desnoyersf2461fc2008-10-06 10:33:00 -0400165
Steven Rostedt3cdfdf92009-02-25 15:54:30 -0500166#define PARAMS(args...) args
Steven Rostedtea20d922009-04-10 08:54:16 -0400167
Steven Rostedtea20d922009-04-10 08:54:16 -0400168#ifndef TRACE_EVENT
Steven Rostedt823f9122009-03-10 12:58:51 -0400169/*
170 * For use with the TRACE_EVENT macro:
171 *
172 * We define a tracepoint, its arguments, its printk format
173 * and its 'fast binay record' layout.
174 *
175 * Firstly, name your tracepoint via TRACE_EVENT(name : the
176 * 'subsystem_event' notation is fine.
177 *
178 * Think about this whole construct as the
179 * 'trace_sched_switch() function' from now on.
180 *
181 *
182 * TRACE_EVENT(sched_switch,
183 *
184 * *
185 * * A function has a regular function arguments
186 * * prototype, declare it via TP_PROTO():
187 * *
188 *
Steven Rostedtef180122009-03-10 14:10:56 -0400189 * TP_PROTO(struct rq *rq, struct task_struct *prev,
190 * struct task_struct *next),
Steven Rostedt823f9122009-03-10 12:58:51 -0400191 *
192 * *
193 * * Define the call signature of the 'function'.
194 * * (Design sidenote: we use this instead of a
195 * * TP_PROTO1/TP_PROTO2/TP_PROTO3 ugliness.)
196 * *
197 *
Steven Rostedtef180122009-03-10 14:10:56 -0400198 * TP_ARGS(rq, prev, next),
Steven Rostedt823f9122009-03-10 12:58:51 -0400199 *
200 * *
201 * * Fast binary tracing: define the trace record via
202 * * TP_STRUCT__entry(). You can think about it like a
203 * * regular C structure local variable definition.
204 * *
205 * * This is how the trace record is structured and will
206 * * be saved into the ring buffer. These are the fields
207 * * that will be exposed to user-space in
GeunSik Lim156f5a72009-06-02 15:01:37 +0900208 * * /sys/kernel/debug/tracing/events/<*>/format.
Steven Rostedt823f9122009-03-10 12:58:51 -0400209 * *
210 * * The declared 'local variable' is called '__entry'
211 * *
212 * * __field(pid_t, prev_prid) is equivalent to a standard declariton:
213 * *
214 * * pid_t prev_pid;
215 * *
216 * * __array(char, prev_comm, TASK_COMM_LEN) is equivalent to:
217 * *
218 * * char prev_comm[TASK_COMM_LEN];
219 * *
220 *
221 * TP_STRUCT__entry(
222 * __array( char, prev_comm, TASK_COMM_LEN )
223 * __field( pid_t, prev_pid )
224 * __field( int, prev_prio )
225 * __array( char, next_comm, TASK_COMM_LEN )
226 * __field( pid_t, next_pid )
227 * __field( int, next_prio )
228 * ),
229 *
230 * *
231 * * Assign the entry into the trace record, by embedding
232 * * a full C statement block into TP_fast_assign(). You
233 * * can refer to the trace record as '__entry' -
234 * * otherwise you can put arbitrary C code in here.
235 * *
236 * * Note: this C code will execute every time a trace event
237 * * happens, on an active tracepoint.
238 * *
239 *
Steven Rostedtef180122009-03-10 14:10:56 -0400240 * TP_fast_assign(
241 * memcpy(__entry->next_comm, next->comm, TASK_COMM_LEN);
242 * __entry->prev_pid = prev->pid;
243 * __entry->prev_prio = prev->prio;
Steven Rostedt823f9122009-03-10 12:58:51 -0400244 * memcpy(__entry->prev_comm, prev->comm, TASK_COMM_LEN);
245 * __entry->next_pid = next->pid;
Steven Rostedtef180122009-03-10 14:10:56 -0400246 * __entry->next_prio = next->prio;
Steven Rostedt823f9122009-03-10 12:58:51 -0400247 * )
248 *
249 * *
250 * * Formatted output of a trace record via TP_printk().
251 * * This is how the tracepoint will appear under ftrace
252 * * plugins that make use of this tracepoint.
253 * *
254 * * (raw-binary tracing wont actually perform this step.)
255 * *
256 *
257 * TP_printk("task %s:%d [%d] ==> %s:%d [%d]",
258 * __entry->prev_comm, __entry->prev_pid, __entry->prev_prio,
259 * __entry->next_comm, __entry->next_pid, __entry->next_prio),
260 *
261 * );
262 *
263 * This macro construct is thus used for the regular printk format
264 * tracing setup, it is used to construct a function pointer based
265 * tracepoint callback (this is used by programmatic plugins and
266 * can also by used by generic instrumentation like SystemTap), and
267 * it is also used to expose a structured trace record in
GeunSik Lim156f5a72009-06-02 15:01:37 +0900268 * /sys/kernel/debug/tracing/events/.
Josh Stone97419872009-08-24 14:43:13 -0700269 *
270 * A set of (un)registration functions can be passed to the variant
271 * TRACE_EVENT_FN to perform any (un)registration work.
Steven Rostedt823f9122009-03-10 12:58:51 -0400272 */
273
Steven Rostedt30a8fec2009-03-10 12:41:38 -0400274#define TRACE_EVENT(name, proto, args, struct, assign, print) \
Steven Rostedtda4d0302009-03-09 17:14:30 -0400275 DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
Josh Stone97419872009-08-24 14:43:13 -0700276#define TRACE_EVENT_FN(name, proto, args, struct, \
277 assign, print, reg, unreg) \
278 DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
Steven Rostedtea20d922009-04-10 08:54:16 -0400279#endif
Steven Rostedtda4d0302009-03-09 17:14:30 -0400280
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400281#endif