Commit | Line | Data |
---|---|---|
27b052e3 PMF |
1 | #ifndef _UST_TRACEPOINT_H |
2 | #define _UST_TRACEPOINT_H | |
f99be407 PMF |
3 | |
4 | /* | |
e9090899 | 5 | * Copyright (C) 2008-2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
1f8b0dff | 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 | ||
f99be407 PMF |
48 | /* |
49 | * it_func[0] is never NULL because there is at least one element in the array | |
50 | * when the array itself is non NULL. | |
51 | */ | |
52 | #define __DO_TRACE(tp, proto, args) \ | |
53 | do { \ | |
b979b346 MD |
54 | struct tracepoint_probe *__tp_it_probe_ptr; \ |
55 | void *__tp_it_func; \ | |
56 | void *__tp_cb_data; \ | |
f99be407 | 57 | \ |
9dec086e | 58 | rcu_read_lock(); \ |
b979b346 MD |
59 | __tp_it_probe_ptr = rcu_dereference((tp)->probes); \ |
60 | if (__tp_it_probe_ptr) { \ | |
f99be407 | 61 | do { \ |
b979b346 MD |
62 | __tp_it_func = __tp_it_probe_ptr->func; \ |
63 | __tp_cb_data = __tp_it_probe_ptr->data; \ | |
64 | ((void(*)(proto))__tp_it_func)(args); \ | |
65 | } while ((++__tp_it_probe_ptr)->func); \ | |
f99be407 | 66 | } \ |
9dec086e | 67 | rcu_read_unlock(); \ |
f99be407 PMF |
68 | } while (0) |
69 | ||
81614639 MD |
70 | #define TP_PARAMS(args...) args |
71 | #define TP_PROTO(args...) args | |
72 | #define TP_ARGS(args...) args | |
73 | ||
f36c12ab | 74 | #define __CHECK_TRACE(name, proto, args) \ |
f99be407 | 75 | do { \ |
f36c12ab MD |
76 | if (unlikely(__tracepoint_##name.state)) \ |
77 | __DO_TRACE(&__tracepoint_##name, \ | |
78 | TP_PROTO(proto), TP_ARGS(args)); \ | |
f99be407 PMF |
79 | } while (0) |
80 | ||
81 | /* | |
82 | * Make sure the alignment of the structure in the __tracepoints section will | |
83 | * not add unwanted padding between the beginning of the section and the | |
84 | * structure. Force alignment to the same alignment as the section start. | |
f99be407 | 85 | */ |
8d71973c | 86 | #define __DECLARE_TRACEPOINT(name, proto, args, data_proto, data_args) \ |
f99be407 | 87 | extern struct tracepoint __tracepoint_##name; \ |
cc7b66ba | 88 | static inline void __trace_##name(proto) \ |
f99be407 | 89 | { \ |
f36c12ab | 90 | __CHECK_TRACE(name, TP_PROTO(data_proto), \ |
9dec086e | 91 | TP_ARGS(data_args)); \ |
f99be407 | 92 | } \ |
9dec086e | 93 | static inline int \ |
cc7b66ba | 94 | __register_trace_##name(void (*probe)(data_proto), void *data) \ |
f99be407 | 95 | { \ |
81614639 | 96 | return __tracepoint_probe_register(#name, (void *)probe,\ |
9dec086e NC |
97 | data); \ |
98 | \ | |
f99be407 | 99 | } \ |
9dec086e | 100 | static inline int \ |
cc7b66ba | 101 | __unregister_trace_##name(void (*probe)(data_proto), void *data)\ |
f99be407 | 102 | { \ |
81614639 | 103 | return __tracepoint_probe_unregister(#name, (void *)probe, \ |
9dec086e | 104 | data); \ |
f99be407 PMF |
105 | } |
106 | ||
332b3a18 | 107 | /* |
81614639 | 108 | * The need for the _DECLARE_TRACEPOINT_NOARGS() is to handle the prototype |
377d33ed | 109 | * (void). "void" is a special value in a function prototype and can |
8d71973c | 110 | * not be combined with other arguments. Since the DECLARE_TRACEPOINT() |
377d33ed MD |
111 | * macro adds a data element at the beginning of the prototype, |
112 | * we need a way to differentiate "(void *data, proto)" from | |
113 | * "(void *data, void)". The second prototype is invalid. | |
114 | * | |
8d71973c | 115 | * DECLARE_TRACEPOINT_NOARGS() passes "void" as the tracepoint prototype |
b979b346 | 116 | * and "void *__tp_cb_data" as the callback prototype. |
377d33ed | 117 | * |
8d71973c | 118 | * DECLARE_TRACEPOINT() passes "proto" as the tracepoint protoype and |
b979b346 | 119 | * "void *__tp_cb_data, proto" as the callback prototype. |
377d33ed | 120 | */ |
81614639 MD |
121 | #define _DECLARE_TRACEPOINT_NOARGS(name) \ |
122 | __DECLARE_TRACEPOINT(name, void, , void *__tp_cb_data, __tp_cb_data) | |
377d33ed | 123 | |
81614639 MD |
124 | #define _DECLARE_TRACEPOINT(name, proto, args) \ |
125 | __DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args), \ | |
126 | TP_PARAMS(void *__tp_cb_data, proto), \ | |
127 | TP_PARAMS(__tp_cb_data, args)) | |
9dec086e | 128 | |
f99be407 | 129 | /* |
81614639 MD |
130 | * __tracepoints_ptrs section is not const (read-only) to let the linker update |
131 | * the pointer, allowing PIC code. | |
f99be407 | 132 | */ |
81614639 MD |
133 | #define _DEFINE_TRACEPOINT(name) \ |
134 | static const char __tpstrtab_##name[] \ | |
135 | __attribute__((section("__tracepoints_strings"))) = #name; \ | |
136 | struct tracepoint __tracepoint_##name \ | |
137 | __attribute__((section("__tracepoints"))) = \ | |
138 | { __tpstrtab_##name, 0, NULL }; \ | |
139 | static struct tracepoint * __tracepoint_ptr_##name \ | |
140 | __attribute__((used, section("__tracepoints_ptrs"))) = \ | |
141 | &__tracepoint_##name; | |
f99be407 | 142 | |
f99be407 | 143 | |
81614639 MD |
144 | #define __register_tracepoint(name, probe, data) \ |
145 | __register_trace_##name(probe, data) | |
146 | #define __unregister_tracepoint(name, probe, data) \ | |
147 | __unregister_trace_##name(probe, data) | |
f99be407 | 148 | |
81614639 MD |
149 | /* |
150 | * Connect a probe to a tracepoint. | |
151 | * Internal API. | |
152 | */ | |
153 | extern | |
154 | int __tracepoint_probe_register(const char *name, void *probe, void *data); | |
f99be407 PMF |
155 | |
156 | /* | |
81614639 MD |
157 | * Disconnect a probe from a tracepoint. |
158 | * Internal API. | |
f99be407 | 159 | */ |
81614639 MD |
160 | extern |
161 | int __tracepoint_probe_unregister(const char *name, void *probe, void *data); | |
f99be407 | 162 | |
81614639 MD |
163 | extern |
164 | int tracepoint_register_lib(struct tracepoint * const *tracepoints_start, | |
165 | int tracepoints_count); | |
166 | extern | |
167 | int tracepoint_unregister_lib(struct tracepoint * const *tracepoints_start); | |
872037bb | 168 | |
6cdf29f8 MD |
169 | /* |
170 | * These weak symbols, the constructor, and destructor take care of | |
171 | * registering only _one_ instance of the tracepoints per shared-ojbect | |
172 | * (or for the whole main program). | |
6cdf29f8 MD |
173 | */ |
174 | extern struct tracepoint * const __start___tracepoints_ptrs[] | |
175 | __attribute__((weak, visibility("hidden"))); | |
176 | extern struct tracepoint * const __stop___tracepoints_ptrs[] | |
177 | __attribute__((weak, visibility("hidden"))); | |
178 | int __tracepoint_registered | |
179 | __attribute__((weak, visibility("hidden"))); | |
180 | ||
181 | static void __attribute__((constructor)) __tracepoints__init(void) | |
182 | { | |
183 | if (__tracepoint_registered++) | |
184 | return; | |
185 | tracepoint_register_lib(__start___tracepoints_ptrs, | |
186 | __stop___tracepoints_ptrs - | |
187 | __start___tracepoints_ptrs); | |
188 | } | |
189 | ||
190 | static void __attribute__((destructor)) __tracepoints__destroy(void) | |
191 | { | |
192 | if (--__tracepoint_registered) | |
193 | return; | |
194 | tracepoint_unregister_lib(__start___tracepoints_ptrs); | |
195 | } | |
22d72948 | 196 | |
8d71973c | 197 | #ifndef TRACEPOINT_EVENT |
22d72948 | 198 | /* |
d75775a6 MD |
199 | * Usage of the TRACEPOINT_EVENT macro: |
200 | * | |
201 | * In short, an example: | |
202 | * | |
d5001b43 | 203 | * TRACEPOINT_EVENT(< [com_company_]project_[component_]event >, |
d75775a6 MD |
204 | * TP_PROTO(int arg0, void *arg1, char *string, size_t strlen, |
205 | * long *arg4, size_t arg4_len), | |
206 | * TP_ARGS(arg0, arg1, string, strlen, arg4, arg4_len), | |
207 | * TP_FIELDS( | |
208 | * | |
209 | * * Integer, printed in base 10 * | |
210 | * ctf_integer(int, field_a, arg0) | |
211 | * | |
212 | * * Integer, printed with 0x base 16 * | |
213 | * ctf_integer_hex(unsigned long, field_d, arg1) | |
214 | * | |
215 | * * Array Sequence, printed as UTF8-encoded array of bytes * | |
216 | * ctf_array_text(char, field_b, string, FIXED_LEN) | |
217 | * ctf_sequence_text(char, field_c, string, size_t, strlen) | |
218 | * | |
219 | * * String, printed as UTF8-encoded string * | |
220 | * ctf_string(field_e, string) | |
221 | * | |
222 | * * Array sequence of signed integer values * | |
223 | * ctf_array(long, field_f, arg4, FIXED_LEN4) | |
224 | * ctf_sequence(long, field_g, arg4, size_t, arg4_len) | |
225 | * ) | |
226 | * ) | |
227 | * | |
228 | * In more detail: | |
22d72948 | 229 | * |
0a42beb6 MD |
230 | * We define a tracepoint, its arguments, and its 'fast binary record' |
231 | * layout. | |
22d72948 | 232 | * |
0a42beb6 | 233 | * Firstly, name your tracepoint via TRACEPOINT_EVENT(name, |
22d72948 | 234 | * |
0a42beb6 MD |
235 | * The "name" MUST follow these rules to ensure no namespace clash |
236 | * occurs: | |
22d72948 | 237 | * |
0a42beb6 MD |
238 | * For projects (applications and libraries) for which an entity |
239 | * specific to the project controls the source code and thus its | |
240 | * tracepoints (typically with a scope larger than a single company): | |
22d72948 | 241 | * |
0a42beb6 MD |
242 | * either: |
243 | * project_component_event | |
244 | * or: | |
245 | * project_event | |
246 | * | |
247 | * Where "project" is the name of the project, | |
d5001b43 MD |
248 | * "component" is the name of the project component (which may |
249 | * include several levels of sub-components, e.g. | |
250 | * ...component_subcomponent_...) where the tracepoint is located | |
251 | * (optional), | |
0a42beb6 MD |
252 | * "event" is the name of the tracepoint event. |
253 | * | |
254 | * For projects issued from a single company wishing to advertise that | |
255 | * the company controls the source code and thus the tracepoints, the | |
256 | * "com_" prefix should be used: | |
257 | * | |
258 | * either: | |
259 | * com_company_project_component_event | |
260 | * or: | |
261 | * com_company_project_event | |
262 | * | |
263 | * Where "company" is the name of the company, | |
264 | * "project" is the name of the project, | |
d5001b43 MD |
265 | * "component" is the name of the project component (which may |
266 | * include several levels of sub-components, e.g. | |
267 | * ...component_subcomponent_...) where the tracepoint is located | |
268 | * (optional), | |
0a42beb6 MD |
269 | * "event" is the name of the tracepoint event. |
270 | * | |
271 | * | |
272 | * As an example, let's consider a user-space application "someproject" | |
273 | * that would have an internal thread scheduler: | |
274 | * | |
275 | * TRACEPOINT_EVENT(someproject_sched_switch, | |
22d72948 NC |
276 | * |
277 | * * | |
278 | * * A function has a regular function arguments | |
279 | * * prototype, declare it via TP_PROTO(): | |
280 | * * | |
281 | * | |
282 | * TP_PROTO(struct rq *rq, struct task_struct *prev, | |
283 | * struct task_struct *next), | |
284 | * | |
285 | * * | |
286 | * * Define the call signature of the 'function'. | |
287 | * * (Design sidenote: we use this instead of a | |
288 | * * TP_PROTO1/TP_PROTO2/TP_PROTO3 ugliness.) | |
289 | * * | |
290 | * | |
291 | * TP_ARGS(rq, prev, next), | |
292 | * | |
293 | * * | |
294 | * * Fast binary tracing: define the trace record via | |
81614639 | 295 | * * TP_FIELDS(). You can think about it like a |
22d72948 NC |
296 | * * regular C structure local variable definition. |
297 | * * | |
298 | * * This is how the trace record is structured and will | |
299 | * * be saved into the ring buffer. These are the fields | |
300 | * * that will be exposed to readers. | |
301 | * * | |
0a42beb6 | 302 | * * ctf_integer(pid_t, prev_pid, prev->pid) is equivalent |
81614639 | 303 | * * to a standard declaraton: |
22d72948 | 304 | * * |
81614639 | 305 | * * pid_t prev_pid; |
22d72948 | 306 | * * |
81614639 | 307 | * * followed by an assignment: |
22d72948 | 308 | * * |
81614639 | 309 | * * prev_pid = prev->pid; |
22d72948 | 310 | * * |
0a42beb6 | 311 | * * ctf_array(char, prev_comm, prev->comm, TASK_COMM_LEN) is |
81614639 | 312 | * * equivalent to: |
22d72948 | 313 | * * |
81614639 | 314 | * * char prev_comm[TASK_COMM_LEN]; |
22d72948 | 315 | * * |
81614639 | 316 | * * followed by an assignment: |
22d72948 | 317 | * * |
81614639 | 318 | * * memcpy(prev_comm, prev->comm, TASK_COMM_LEN); |
22d72948 NC |
319 | * * |
320 | * | |
81614639 | 321 | * TP_FIELDS( |
0a42beb6 MD |
322 | * ctf_array(char, prev_comm, prev->comm, TASK_COMM_LEN) |
323 | * ctf_integer(pid_t, prev_pid, prev->pid) | |
324 | * ctf_integer(int, prev_prio, prev->prio) | |
325 | * ctf_array(char, next_comm, next->comm, TASK_COMM_LEN) | |
326 | * ctf_integer(pid_t, next_pid, next->pid) | |
327 | * ctf_integer(int, next_prio, next->prio) | |
22d72948 | 328 | * ) |
0a42beb6 | 329 | * ) |
22d72948 NC |
330 | */ |
331 | ||
81614639 MD |
332 | #define TRACEPOINT_EVENT(name, proto, args, fields) \ |
333 | _DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args)) | |
0c0686ee | 334 | |
81614639 MD |
335 | #define TRACEPOINT_EVENT_CLASS(name, proto, args, fields) |
336 | #define TRACEPOINT_EVENT_INSTANCE(template, name, proto, args) \ | |
337 | _DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args)) | |
0c0686ee | 338 | |
81614639 MD |
339 | /* |
340 | * Declaration of tracepoints that take 0 argument. | |
341 | */ | |
342 | #define TRACEPOINT_EVENT_NOARGS(name, fields) \ | |
343 | _DECLARE_TRACEPOINT_NOARGS(name) | |
0c0686ee | 344 | |
81614639 MD |
345 | #define TRACEPOINT_EVENT_CLASS_NOARGS(name, fields) |
346 | #define TRACEPOINT_EVENT_INSTANCE_NOARGS(template, name) \ | |
347 | _DECLARE_TRACEPOINT_NOARGS(name) | |
0c0686ee | 348 | |
81614639 | 349 | #endif /* #ifndef TRACEPOINT_EVENT */ |
22d72948 | 350 | |
27b052e3 | 351 | #endif /* _UST_TRACEPOINT_H */ |