Commit | Line | Data |
---|---|---|
97e1c18e MD |
1 | #ifndef _LINUX_TRACEPOINT_H |
2 | #define _LINUX_TRACEPOINT_H | |
3 | ||
4 | /* | |
5 | * Kernel Tracepoint API. | |
6 | * | |
8cd09a59 | 7 | * See Documentation/trace/tracepoints.txt. |
97e1c18e MD |
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. */ | |
97419875 JS |
26 | void (*regfunc)(void); |
27 | void (*unregfunc)(void); | |
97e1c18e | 28 | void **funcs; |
7e066fb8 MD |
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 | */ | |
97e1c18e | 35 | |
ea20d929 SR |
36 | #ifndef DECLARE_TRACE |
37 | ||
2939b046 | 38 | #define TP_PROTO(args...) args |
8cd09a59 | 39 | #define TP_ARGS(args...) args |
97e1c18e MD |
40 | |
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 | \ | |
da7b3eab | 51 | rcu_read_lock_sched_notrace(); \ |
97e1c18e MD |
52 | it_func = rcu_dereference((tp)->funcs); \ |
53 | if (it_func) { \ | |
54 | do { \ | |
55 | ((void(*)(proto))(*it_func))(args); \ | |
56 | } while (*(++it_func)); \ | |
57 | } \ | |
da7b3eab | 58 | rcu_read_unlock_sched_notrace(); \ |
97e1c18e MD |
59 | } 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 | */ | |
97419875 | 66 | #define DECLARE_TRACE(name, proto, args) \ |
7e066fb8 | 67 | extern struct tracepoint __tracepoint_##name; \ |
97e1c18e MD |
68 | static inline void trace_##name(proto) \ |
69 | { \ | |
97e1c18e MD |
70 | if (unlikely(__tracepoint_##name.state)) \ |
71 | __DO_TRACE(&__tracepoint_##name, \ | |
2939b046 | 72 | TP_PROTO(proto), TP_ARGS(args)); \ |
97e1c18e MD |
73 | } \ |
74 | static inline int register_trace_##name(void (*probe)(proto)) \ | |
75 | { \ | |
97419875 | 76 | return tracepoint_probe_register(#name, (void *)probe); \ |
97e1c18e | 77 | } \ |
c420970e | 78 | static inline int unregister_trace_##name(void (*probe)(proto)) \ |
97e1c18e | 79 | { \ |
97419875 | 80 | return tracepoint_probe_unregister(#name, (void *)probe);\ |
97e1c18e MD |
81 | } |
82 | ||
63fbdab3 | 83 | |
97419875 | 84 | #define DEFINE_TRACE_FN(name, reg, unreg) \ |
7e066fb8 MD |
85 | static const char __tpstrtab_##name[] \ |
86 | __attribute__((section("__tracepoints_strings"))) = #name; \ | |
87 | struct tracepoint __tracepoint_##name \ | |
88 | __attribute__((section("__tracepoints"), aligned(32))) = \ | |
97419875 JS |
89 | { __tpstrtab_##name, 0, reg, unreg, NULL } |
90 | ||
91 | #define DEFINE_TRACE(name) \ | |
92 | DEFINE_TRACE_FN(name, NULL, NULL); | |
7e066fb8 MD |
93 | |
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 | ||
97e1c18e MD |
99 | extern void tracepoint_update_probe_range(struct tracepoint *begin, |
100 | struct tracepoint *end); | |
101 | ||
102 | #else /* !CONFIG_TRACEPOINTS */ | |
97419875 | 103 | #define DECLARE_TRACE(name, proto, args) \ |
97e1c18e MD |
104 | 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 | } \ | |
c420970e MD |
112 | static inline int unregister_trace_##name(void (*probe)(proto)) \ |
113 | { \ | |
114 | return -ENOSYS; \ | |
115 | } | |
97e1c18e | 116 | |
97419875 | 117 | #define DEFINE_TRACE_FN(name, reg, unreg) |
7e066fb8 MD |
118 | #define DEFINE_TRACE(name) |
119 | #define EXPORT_TRACEPOINT_SYMBOL_GPL(name) | |
120 | #define EXPORT_TRACEPOINT_SYMBOL(name) | |
121 | ||
97e1c18e MD |
122 | static inline void tracepoint_update_probe_range(struct tracepoint *begin, |
123 | struct tracepoint *end) | |
124 | { } | |
125 | #endif /* CONFIG_TRACEPOINTS */ | |
ea20d929 | 126 | #endif /* DECLARE_TRACE */ |
97e1c18e MD |
127 | |
128 | /* | |
129 | * Connect a probe to a tracepoint. | |
130 | * Internal API, should not be used directly. | |
131 | */ | |
132 | extern 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 | */ | |
138 | extern int tracepoint_probe_unregister(const char *name, void *probe); | |
139 | ||
127cafbb LJ |
140 | extern int tracepoint_probe_register_noupdate(const char *name, void *probe); |
141 | extern int tracepoint_probe_unregister_noupdate(const char *name, void *probe); | |
142 | extern void tracepoint_probe_update_all(void); | |
143 | ||
97e1c18e MD |
144 | struct tracepoint_iter { |
145 | struct module *module; | |
146 | struct tracepoint *tracepoint; | |
147 | }; | |
148 | ||
149 | extern void tracepoint_iter_start(struct tracepoint_iter *iter); | |
150 | extern void tracepoint_iter_next(struct tracepoint_iter *iter); | |
151 | extern void tracepoint_iter_stop(struct tracepoint_iter *iter); | |
152 | extern void tracepoint_iter_reset(struct tracepoint_iter *iter); | |
153 | extern int tracepoint_get_iter_range(struct tracepoint **tracepoint, | |
154 | struct tracepoint *begin, struct tracepoint *end); | |
155 | ||
f2461fc8 MD |
156 | /* |
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 | */ | |
231375cc MD |
161 | static inline void tracepoint_synchronize_unregister(void) |
162 | { | |
163 | synchronize_sched(); | |
164 | } | |
f2461fc8 | 165 | |
3cdfdf91 | 166 | #define PARAMS(args...) args |
7cb2e3ee SR |
167 | |
168 | #endif /* _LINUX_TRACEPOINT_H */ | |
169 | ||
170 | /* | |
171 | * Note: we keep the TRACE_EVENT outside the include file ifdef protection. | |
172 | * This is due to the way trace events work. If a file includes two | |
173 | * trace event headers under one "CREATE_TRACE_POINTS" the first include | |
174 | * will override the TRACE_EVENT and break the second include. | |
175 | */ | |
ea20d929 | 176 | |
ea20d929 | 177 | #ifndef TRACE_EVENT |
823f9124 SR |
178 | /* |
179 | * For use with the TRACE_EVENT macro: | |
180 | * | |
181 | * We define a tracepoint, its arguments, its printk format | |
182 | * and its 'fast binay record' layout. | |
183 | * | |
184 | * Firstly, name your tracepoint via TRACE_EVENT(name : the | |
185 | * 'subsystem_event' notation is fine. | |
186 | * | |
187 | * Think about this whole construct as the | |
188 | * 'trace_sched_switch() function' from now on. | |
189 | * | |
190 | * | |
191 | * TRACE_EVENT(sched_switch, | |
192 | * | |
193 | * * | |
194 | * * A function has a regular function arguments | |
195 | * * prototype, declare it via TP_PROTO(): | |
196 | * * | |
197 | * | |
ef18012b SR |
198 | * TP_PROTO(struct rq *rq, struct task_struct *prev, |
199 | * struct task_struct *next), | |
823f9124 SR |
200 | * |
201 | * * | |
202 | * * Define the call signature of the 'function'. | |
203 | * * (Design sidenote: we use this instead of a | |
204 | * * TP_PROTO1/TP_PROTO2/TP_PROTO3 ugliness.) | |
205 | * * | |
206 | * | |
ef18012b | 207 | * TP_ARGS(rq, prev, next), |
823f9124 SR |
208 | * |
209 | * * | |
210 | * * Fast binary tracing: define the trace record via | |
211 | * * TP_STRUCT__entry(). You can think about it like a | |
212 | * * regular C structure local variable definition. | |
213 | * * | |
214 | * * This is how the trace record is structured and will | |
215 | * * be saved into the ring buffer. These are the fields | |
216 | * * that will be exposed to user-space in | |
156f5a78 | 217 | * * /sys/kernel/debug/tracing/events/<*>/format. |
823f9124 SR |
218 | * * |
219 | * * The declared 'local variable' is called '__entry' | |
220 | * * | |
221 | * * __field(pid_t, prev_prid) is equivalent to a standard declariton: | |
222 | * * | |
223 | * * pid_t prev_pid; | |
224 | * * | |
225 | * * __array(char, prev_comm, TASK_COMM_LEN) is equivalent to: | |
226 | * * | |
227 | * * char prev_comm[TASK_COMM_LEN]; | |
228 | * * | |
229 | * | |
230 | * TP_STRUCT__entry( | |
231 | * __array( char, prev_comm, TASK_COMM_LEN ) | |
232 | * __field( pid_t, prev_pid ) | |
233 | * __field( int, prev_prio ) | |
234 | * __array( char, next_comm, TASK_COMM_LEN ) | |
235 | * __field( pid_t, next_pid ) | |
236 | * __field( int, next_prio ) | |
237 | * ), | |
238 | * | |
239 | * * | |
240 | * * Assign the entry into the trace record, by embedding | |
241 | * * a full C statement block into TP_fast_assign(). You | |
242 | * * can refer to the trace record as '__entry' - | |
243 | * * otherwise you can put arbitrary C code in here. | |
244 | * * | |
245 | * * Note: this C code will execute every time a trace event | |
246 | * * happens, on an active tracepoint. | |
247 | * * | |
248 | * | |
ef18012b SR |
249 | * TP_fast_assign( |
250 | * memcpy(__entry->next_comm, next->comm, TASK_COMM_LEN); | |
251 | * __entry->prev_pid = prev->pid; | |
252 | * __entry->prev_prio = prev->prio; | |
823f9124 SR |
253 | * memcpy(__entry->prev_comm, prev->comm, TASK_COMM_LEN); |
254 | * __entry->next_pid = next->pid; | |
ef18012b | 255 | * __entry->next_prio = next->prio; |
823f9124 SR |
256 | * ) |
257 | * | |
258 | * * | |
259 | * * Formatted output of a trace record via TP_printk(). | |
260 | * * This is how the tracepoint will appear under ftrace | |
261 | * * plugins that make use of this tracepoint. | |
262 | * * | |
263 | * * (raw-binary tracing wont actually perform this step.) | |
264 | * * | |
265 | * | |
266 | * TP_printk("task %s:%d [%d] ==> %s:%d [%d]", | |
267 | * __entry->prev_comm, __entry->prev_pid, __entry->prev_prio, | |
268 | * __entry->next_comm, __entry->next_pid, __entry->next_prio), | |
269 | * | |
270 | * ); | |
271 | * | |
272 | * This macro construct is thus used for the regular printk format | |
273 | * tracing setup, it is used to construct a function pointer based | |
274 | * tracepoint callback (this is used by programmatic plugins and | |
275 | * can also by used by generic instrumentation like SystemTap), and | |
276 | * it is also used to expose a structured trace record in | |
156f5a78 | 277 | * /sys/kernel/debug/tracing/events/. |
97419875 JS |
278 | * |
279 | * A set of (un)registration functions can be passed to the variant | |
280 | * TRACE_EVENT_FN to perform any (un)registration work. | |
823f9124 SR |
281 | */ |
282 | ||
091ad365 | 283 | #define DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, print) |
ff038f5c SR |
284 | #define DEFINE_EVENT(template, name, proto, args) \ |
285 | DECLARE_TRACE(name, PARAMS(proto), PARAMS(args)) | |
e5bc9721 SR |
286 | #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \ |
287 | DECLARE_TRACE(name, PARAMS(proto), PARAMS(args)) | |
ff038f5c | 288 | |
30a8fecc | 289 | #define TRACE_EVENT(name, proto, args, struct, assign, print) \ |
da4d0302 | 290 | DECLARE_TRACE(name, PARAMS(proto), PARAMS(args)) |
97419875 JS |
291 | #define TRACE_EVENT_FN(name, proto, args, struct, \ |
292 | assign, print, reg, unreg) \ | |
293 | DECLARE_TRACE(name, PARAMS(proto), PARAMS(args)) | |
7cb2e3ee SR |
294 | |
295 | #endif /* ifdef TRACE_EVENT (see note above) */ |