Commit | Line | Data |
---|---|---|
27b052e3 PMF |
1 | #ifndef _UST_TRACEPOINT_H |
2 | #define _UST_TRACEPOINT_H | |
f99be407 PMF |
3 | |
4 | /* | |
1f8b0dff PMF |
5 | * Copyright (C) 2008 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> |
6 | * Copyright (C) 2009 Pierre-Marc Fournier | |
22d72948 | 7 | * Copyright (C) 2009 Steven Rostedt <rostedt@goodmis.org> |
f99be407 | 8 | * |
8fc2d8db PMF |
9 | * This library is free software; you can redistribute it and/or |
10 | * modify it under the terms of the GNU Lesser General Public | |
f37142a4 MD |
11 | * License as published by the Free Software Foundation; |
12 | * version 2.1 of the License. | |
f99be407 | 13 | * |
8fc2d8db | 14 | * This library is distributed in the hope that it will be useful, |
1f8b0dff | 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
8fc2d8db PMF |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 | * Lesser General Public License for more details. | |
1f8b0dff | 18 | * |
8fc2d8db PMF |
19 | * You should have received a copy of the GNU Lesser General Public |
20 | * License along with this library; if not, write to the Free Software | |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
f99be407 PMF |
22 | * |
23 | * Heavily inspired from the Linux Kernel Markers. | |
24 | * | |
1f8b0dff | 25 | * Ported to userspace by Pierre-Marc Fournier. |
f99be407 PMF |
26 | */ |
27 | ||
b7ea1a1c | 28 | #include <urcu-bp.h> |
b0c4126f | 29 | #include <urcu/list.h> |
f99be407 | 30 | |
b979b346 | 31 | struct tracepoint_probe { |
9dec086e NC |
32 | void *func; |
33 | void *data; | |
34 | }; | |
35 | ||
f99be407 PMF |
36 | struct tracepoint { |
37 | const char *name; /* Tracepoint name */ | |
f36c12ab | 38 | char state; /* State. */ |
b979b346 | 39 | struct tracepoint_probe *probes; |
f218ff28 | 40 | }; |
f99be407 | 41 | |
cc7b66ba MD |
42 | /* |
43 | * Tracepoints should be added to the instrumented code using the | |
44 | * "tracepoint()" macro. | |
45 | */ | |
46 | #define tracepoint(name, args...) __trace_##name(args) | |
47 | ||
81614639 MD |
48 | /* |
49 | * Library should be made known to libust by declaring TRACEPOINT_LIB in | |
50 | * the source file. (Usually at the end of the file, in the outermost | |
51 | * scope). | |
52 | */ | |
53 | #define TRACEPOINT_LIB \ | |
54 | extern struct tracepoint * const __start___tracepoints_ptrs[] __attribute__((weak, visibility("hidden"))); \ | |
55 | extern struct tracepoint * const __stop___tracepoints_ptrs[] __attribute__((weak, visibility("hidden"))); \ | |
56 | static struct tracepoint * __tracepoint_ptr_dummy \ | |
57 | __attribute__((used, section("__tracepoints_ptrs"))); \ | |
58 | static void __attribute__((constructor)) __tracepoints__init(void) \ | |
59 | { \ | |
60 | tracepoint_register_lib(__start___tracepoints_ptrs, \ | |
61 | __stop___tracepoints_ptrs - \ | |
62 | __start___tracepoints_ptrs); \ | |
63 | } \ | |
64 | \ | |
65 | static void __attribute__((destructor)) __tracepoints__destroy(void) \ | |
66 | { \ | |
67 | tracepoint_unregister_lib(__start___tracepoints_ptrs); \ | |
68 | } | |
f99be407 PMF |
69 | |
70 | /* | |
71 | * it_func[0] is never NULL because there is at least one element in the array | |
72 | * when the array itself is non NULL. | |
73 | */ | |
74 | #define __DO_TRACE(tp, proto, args) \ | |
75 | do { \ | |
b979b346 MD |
76 | struct tracepoint_probe *__tp_it_probe_ptr; \ |
77 | void *__tp_it_func; \ | |
78 | void *__tp_cb_data; \ | |
f99be407 | 79 | \ |
9dec086e | 80 | rcu_read_lock(); \ |
b979b346 MD |
81 | __tp_it_probe_ptr = rcu_dereference((tp)->probes); \ |
82 | if (__tp_it_probe_ptr) { \ | |
f99be407 | 83 | do { \ |
b979b346 MD |
84 | __tp_it_func = __tp_it_probe_ptr->func; \ |
85 | __tp_cb_data = __tp_it_probe_ptr->data; \ | |
86 | ((void(*)(proto))__tp_it_func)(args); \ | |
87 | } while ((++__tp_it_probe_ptr)->func); \ | |
f99be407 | 88 | } \ |
9dec086e | 89 | rcu_read_unlock(); \ |
f99be407 PMF |
90 | } while (0) |
91 | ||
81614639 MD |
92 | #define TP_PARAMS(args...) args |
93 | #define TP_PROTO(args...) args | |
94 | #define TP_ARGS(args...) args | |
95 | ||
f36c12ab | 96 | #define __CHECK_TRACE(name, proto, args) \ |
f99be407 | 97 | do { \ |
f36c12ab MD |
98 | if (unlikely(__tracepoint_##name.state)) \ |
99 | __DO_TRACE(&__tracepoint_##name, \ | |
100 | TP_PROTO(proto), TP_ARGS(args)); \ | |
f99be407 PMF |
101 | } while (0) |
102 | ||
103 | /* | |
104 | * Make sure the alignment of the structure in the __tracepoints section will | |
105 | * not add unwanted padding between the beginning of the section and the | |
106 | * structure. Force alignment to the same alignment as the section start. | |
f99be407 | 107 | */ |
8d71973c | 108 | #define __DECLARE_TRACEPOINT(name, proto, args, data_proto, data_args) \ |
f99be407 | 109 | extern struct tracepoint __tracepoint_##name; \ |
cc7b66ba | 110 | static inline void __trace_##name(proto) \ |
f99be407 | 111 | { \ |
f36c12ab | 112 | __CHECK_TRACE(name, TP_PROTO(data_proto), \ |
9dec086e | 113 | TP_ARGS(data_args)); \ |
f99be407 | 114 | } \ |
9dec086e | 115 | static inline int \ |
cc7b66ba | 116 | __register_trace_##name(void (*probe)(data_proto), void *data) \ |
f99be407 | 117 | { \ |
81614639 | 118 | return __tracepoint_probe_register(#name, (void *)probe,\ |
9dec086e NC |
119 | data); \ |
120 | \ | |
f99be407 | 121 | } \ |
9dec086e | 122 | static inline int \ |
cc7b66ba | 123 | __unregister_trace_##name(void (*probe)(data_proto), void *data)\ |
f99be407 | 124 | { \ |
81614639 | 125 | return __tracepoint_probe_unregister(#name, (void *)probe, \ |
9dec086e | 126 | data); \ |
2874fee5 MD |
127 | } \ |
128 | /* \ | |
129 | * Backward-compatibility API (will be deprecated): \ | |
130 | * trace_* \ | |
131 | * register_trace_* \ | |
132 | * unregister_trace_* \ | |
133 | */ \ | |
134 | static inline void trace_##name(proto) \ | |
135 | { \ | |
136 | __CHECK_TRACE(name, TP_PROTO(proto), \ | |
137 | TP_ARGS(args)); \ | |
138 | } \ | |
139 | static inline int \ | |
140 | register_trace_##name(void (*probe)(proto)) \ | |
141 | { \ | |
142 | return __tracepoint_probe_register(#name, (void *)probe,\ | |
143 | NULL); \ | |
144 | } \ | |
145 | static inline int \ | |
146 | unregister_trace_##name(void (*probe)(proto)) \ | |
147 | { \ | |
148 | return __tracepoint_probe_unregister(#name, (void *)probe, \ | |
149 | NULL); \ | |
f99be407 PMF |
150 | } |
151 | ||
332b3a18 | 152 | /* |
81614639 | 153 | * The need for the _DECLARE_TRACEPOINT_NOARGS() is to handle the prototype |
377d33ed | 154 | * (void). "void" is a special value in a function prototype and can |
8d71973c | 155 | * not be combined with other arguments. Since the DECLARE_TRACEPOINT() |
377d33ed MD |
156 | * macro adds a data element at the beginning of the prototype, |
157 | * we need a way to differentiate "(void *data, proto)" from | |
158 | * "(void *data, void)". The second prototype is invalid. | |
159 | * | |
8d71973c | 160 | * DECLARE_TRACEPOINT_NOARGS() passes "void" as the tracepoint prototype |
b979b346 | 161 | * and "void *__tp_cb_data" as the callback prototype. |
377d33ed | 162 | * |
8d71973c | 163 | * DECLARE_TRACEPOINT() passes "proto" as the tracepoint protoype and |
b979b346 | 164 | * "void *__tp_cb_data, proto" as the callback prototype. |
377d33ed | 165 | */ |
81614639 MD |
166 | #define _DECLARE_TRACEPOINT_NOARGS(name) \ |
167 | __DECLARE_TRACEPOINT(name, void, , void *__tp_cb_data, __tp_cb_data) | |
377d33ed | 168 | |
81614639 MD |
169 | #define _DECLARE_TRACEPOINT(name, proto, args) \ |
170 | __DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args), \ | |
171 | TP_PARAMS(void *__tp_cb_data, proto), \ | |
172 | TP_PARAMS(__tp_cb_data, args)) | |
9dec086e | 173 | |
f99be407 | 174 | /* |
81614639 MD |
175 | * __tracepoints_ptrs section is not const (read-only) to let the linker update |
176 | * the pointer, allowing PIC code. | |
f99be407 | 177 | */ |
81614639 MD |
178 | #define _DEFINE_TRACEPOINT(name) \ |
179 | static const char __tpstrtab_##name[] \ | |
180 | __attribute__((section("__tracepoints_strings"))) = #name; \ | |
181 | struct tracepoint __tracepoint_##name \ | |
182 | __attribute__((section("__tracepoints"))) = \ | |
183 | { __tpstrtab_##name, 0, NULL }; \ | |
184 | static struct tracepoint * __tracepoint_ptr_##name \ | |
185 | __attribute__((used, section("__tracepoints_ptrs"))) = \ | |
186 | &__tracepoint_##name; | |
f99be407 | 187 | |
f99be407 | 188 | |
81614639 MD |
189 | #define __register_tracepoint(name, probe, data) \ |
190 | __register_trace_##name(probe, data) | |
191 | #define __unregister_tracepoint(name, probe, data) \ | |
192 | __unregister_trace_##name(probe, data) | |
f99be407 | 193 | |
81614639 MD |
194 | /* |
195 | * Connect a probe to a tracepoint. | |
196 | * Internal API. | |
197 | */ | |
198 | extern | |
199 | int __tracepoint_probe_register(const char *name, void *probe, void *data); | |
f99be407 PMF |
200 | |
201 | /* | |
81614639 MD |
202 | * Disconnect a probe from a tracepoint. |
203 | * Internal API. | |
f99be407 | 204 | */ |
81614639 MD |
205 | extern |
206 | int __tracepoint_probe_unregister(const char *name, void *probe, void *data); | |
f99be407 | 207 | |
474d745f | 208 | struct tracepoint_lib { |
f218ff28 | 209 | struct tracepoint * const *tracepoints_start; |
474d745f | 210 | int tracepoints_count; |
0222e121 | 211 | struct cds_list_head list; |
474d745f PMF |
212 | }; |
213 | ||
81614639 MD |
214 | extern |
215 | int tracepoint_register_lib(struct tracepoint * const *tracepoints_start, | |
216 | int tracepoints_count); | |
217 | extern | |
218 | int tracepoint_unregister_lib(struct tracepoint * const *tracepoints_start); | |
872037bb | 219 | |
2874fee5 MD |
220 | /* |
221 | * Backward-compatibility API (will be deprecated): | |
222 | * DEFINE_TRACE | |
223 | * DECLARE_TRACE | |
224 | * register_tracepoint | |
225 | * unregister_tracepoint | |
226 | */ | |
227 | #define DEFINE_TRACE _DEFINE_TRACEPOINT | |
228 | #define DECLARE_TRACE(name, proto, args) \ | |
229 | _DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args)) | |
22d72948 | 230 | |
8d71973c | 231 | #ifndef TRACEPOINT_EVENT |
22d72948 | 232 | /* |
8d71973c | 233 | * For use with the TRACEPOINT_EVENT macro: |
22d72948 NC |
234 | * |
235 | * We define a tracepoint, its arguments, its printf format | |
236 | * and its 'fast binary record' layout. | |
237 | * | |
8d71973c | 238 | * Firstly, name your tracepoint via TRACEPOINT_EVENT(name : the |
22d72948 NC |
239 | * 'subsystem_event' notation is fine. |
240 | * | |
241 | * Think about this whole construct as the | |
242 | * 'trace_sched_switch() function' from now on. | |
243 | * | |
244 | * | |
8d71973c | 245 | * TRACEPOINT_EVENT(sched_switch, |
22d72948 NC |
246 | * |
247 | * * | |
248 | * * A function has a regular function arguments | |
249 | * * prototype, declare it via TP_PROTO(): | |
250 | * * | |
251 | * | |
252 | * TP_PROTO(struct rq *rq, struct task_struct *prev, | |
253 | * struct task_struct *next), | |
254 | * | |
255 | * * | |
256 | * * Define the call signature of the 'function'. | |
257 | * * (Design sidenote: we use this instead of a | |
258 | * * TP_PROTO1/TP_PROTO2/TP_PROTO3 ugliness.) | |
259 | * * | |
260 | * | |
261 | * TP_ARGS(rq, prev, next), | |
262 | * | |
263 | * * | |
264 | * * Fast binary tracing: define the trace record via | |
81614639 | 265 | * * TP_FIELDS(). You can think about it like a |
22d72948 NC |
266 | * * regular C structure local variable definition. |
267 | * * | |
268 | * * This is how the trace record is structured and will | |
269 | * * be saved into the ring buffer. These are the fields | |
270 | * * that will be exposed to readers. | |
271 | * * | |
81614639 MD |
272 | * * tp_field(pid_t, prev_pid, prev->pid) is equivalent |
273 | * * to a standard declaraton: | |
22d72948 | 274 | * * |
81614639 | 275 | * * pid_t prev_pid; |
22d72948 | 276 | * * |
81614639 | 277 | * * followed by an assignment: |
22d72948 | 278 | * * |
81614639 | 279 | * * prev_pid = prev->pid; |
22d72948 | 280 | * * |
81614639 MD |
281 | * * tp_array(char, prev_comm, TASK_COMM_LEN, prev->comm) is |
282 | * * equivalent to: | |
22d72948 | 283 | * * |
81614639 | 284 | * * char prev_comm[TASK_COMM_LEN]; |
22d72948 | 285 | * * |
81614639 | 286 | * * followed by an assignment: |
22d72948 | 287 | * * |
81614639 | 288 | * * memcpy(prev_comm, prev->comm, TASK_COMM_LEN); |
22d72948 NC |
289 | * * |
290 | * | |
81614639 MD |
291 | * TP_FIELDS( |
292 | * tp_array(char, prev_comm, TASK_COMM_LEN, prev->comm) | |
293 | * tp_field(pid_t, prev_pid, prev->pid) | |
294 | * tp_field(int, prev_prio, prev->prio) | |
295 | * tp_array(char, next_comm, TASK_COMM_LEN, next->comm) | |
296 | * tp_field(pid_t, next_pid, next->pid) | |
297 | * tp_field(int, next_prio, next->prio) | |
22d72948 | 298 | * ) |
22d72948 | 299 | * ); |
22d72948 NC |
300 | */ |
301 | ||
81614639 MD |
302 | #define TRACEPOINT_EVENT(name, proto, args, fields) \ |
303 | _DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args)) | |
0c0686ee | 304 | |
81614639 MD |
305 | #define TRACEPOINT_EVENT_CLASS(name, proto, args, fields) |
306 | #define TRACEPOINT_EVENT_INSTANCE(template, name, proto, args) \ | |
307 | _DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args)) | |
0c0686ee | 308 | |
81614639 MD |
309 | /* |
310 | * Declaration of tracepoints that take 0 argument. | |
311 | */ | |
312 | #define TRACEPOINT_EVENT_NOARGS(name, fields) \ | |
313 | _DECLARE_TRACEPOINT_NOARGS(name) | |
0c0686ee | 314 | |
81614639 MD |
315 | #define TRACEPOINT_EVENT_CLASS_NOARGS(name, fields) |
316 | #define TRACEPOINT_EVENT_INSTANCE_NOARGS(template, name) \ | |
317 | _DECLARE_TRACEPOINT_NOARGS(name) | |
0c0686ee | 318 | |
0c0686ee | 319 | |
0c0686ee | 320 | |
8d71973c | 321 | #define TRACEPOINT_EVENT_LIB \ |
fc1caebc | 322 | extern struct trace_event * const __start___trace_events_ptrs[] \ |
0c0686ee | 323 | __attribute__((weak, visibility("hidden"))); \ |
fc1caebc | 324 | extern struct trace_event * const __stop___trace_events_ptrs[] \ |
0c0686ee | 325 | __attribute__((weak, visibility("hidden"))); \ |
332b3a18 MD |
326 | static struct trace_event * __event_ptrs_dummy \ |
327 | __attribute__((used, section("__trace_events_ptrs"))); \ | |
0c0686ee NC |
328 | static void __attribute__((constructor)) \ |
329 | __trace_events__init(void) \ | |
330 | { \ | |
fc1caebc MD |
331 | trace_event_register_lib(__start___trace_events_ptrs, \ |
332 | __stop___trace_events_ptrs - \ | |
333 | __start___trace_events_ptrs); \ | |
0c0686ee NC |
334 | } \ |
335 | \ | |
336 | static void __attribute__((destructor)) \ | |
337 | __trace_event__destroy(void) \ | |
338 | { \ | |
fc1caebc | 339 | trace_event_unregister_lib(__start___trace_events_ptrs);\ |
0c0686ee NC |
340 | } |
341 | ||
81614639 MD |
342 | struct trace_event { |
343 | const char *name; | |
344 | }; | |
22d72948 | 345 | |
81614639 MD |
346 | struct trace_event_lib { |
347 | struct trace_event * const *trace_events_start; | |
348 | int trace_events_count; | |
349 | struct cds_list_head list; | |
350 | }; | |
22d72948 | 351 | |
81614639 MD |
352 | extern |
353 | int trace_event_register_lib(struct trace_event * const *start_trace_events, | |
354 | int trace_event_count); | |
355 | extern | |
356 | int trace_event_unregister_lib(struct trace_event * const *start_trace_events); | |
22d72948 | 357 | |
81614639 | 358 | #endif /* #ifndef TRACEPOINT_EVENT */ |
22d72948 | 359 | |
27b052e3 | 360 | #endif /* _UST_TRACEPOINT_H */ |