Commit | Line | Data |
---|---|---|
51489cad MD |
1 | #ifndef _LTTNG_TRACEPOINT_H |
2 | #define _LTTNG_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. | |
f99be407 PMF |
24 | */ |
25 | ||
b7ea1a1c | 26 | #include <urcu-bp.h> |
b0c4126f | 27 | #include <urcu/list.h> |
51489cad | 28 | #include <lttng/tracepoint-types.h> |
f99be407 | 29 | |
1ea2c2df MD |
30 | #ifdef __cplusplus |
31 | extern "C" { | |
32 | #endif | |
33 | ||
cc7b66ba MD |
34 | /* |
35 | * Tracepoints should be added to the instrumented code using the | |
36 | * "tracepoint()" macro. | |
37 | */ | |
7083f0fe MD |
38 | #define tracepoint(provider, name, args...) \ |
39 | __trace_##provider##___##name(args) | |
cc7b66ba | 40 | |
f99be407 PMF |
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 { \ | |
b979b346 MD |
47 | struct tracepoint_probe *__tp_it_probe_ptr; \ |
48 | void *__tp_it_func; \ | |
49 | void *__tp_cb_data; \ | |
f99be407 | 50 | \ |
9dec086e | 51 | rcu_read_lock(); \ |
b979b346 MD |
52 | __tp_it_probe_ptr = rcu_dereference((tp)->probes); \ |
53 | if (__tp_it_probe_ptr) { \ | |
f99be407 | 54 | do { \ |
b979b346 MD |
55 | __tp_it_func = __tp_it_probe_ptr->func; \ |
56 | __tp_cb_data = __tp_it_probe_ptr->data; \ | |
1ea2c2df | 57 | URCU_FORCE_CAST(void(*)(proto), __tp_it_func)(args); \ |
b979b346 | 58 | } while ((++__tp_it_probe_ptr)->func); \ |
f99be407 | 59 | } \ |
9dec086e | 60 | rcu_read_unlock(); \ |
f99be407 PMF |
61 | } while (0) |
62 | ||
81614639 MD |
63 | #define TP_PARAMS(args...) args |
64 | #define TP_PROTO(args...) args | |
65 | #define TP_ARGS(args...) args | |
66 | ||
7083f0fe | 67 | #define __CHECK_TRACE(provider, name, proto, args) \ |
f99be407 | 68 | do { \ |
7083f0fe MD |
69 | if (caa_unlikely(__tracepoint_##provider##___##name.state)) \ |
70 | __DO_TRACE(&__tracepoint_##provider##___##name, \ | |
f36c12ab | 71 | TP_PROTO(proto), TP_ARGS(args)); \ |
f99be407 PMF |
72 | } while (0) |
73 | ||
74 | /* | |
75 | * Make sure the alignment of the structure in the __tracepoints section will | |
76 | * not add unwanted padding between the beginning of the section and the | |
77 | * structure. Force alignment to the same alignment as the section start. | |
f99be407 | 78 | */ |
7083f0fe MD |
79 | #define __DECLARE_TRACEPOINT(provider, name, proto, args, data_proto, data_args) \ |
80 | extern struct tracepoint __tracepoint_##provider##___##name; \ | |
81 | static inline void __trace_##provider##___##name(proto) \ | |
f99be407 | 82 | { \ |
7083f0fe | 83 | __CHECK_TRACE(provider, name, TP_PROTO(data_proto), \ |
9dec086e | 84 | TP_ARGS(data_args)); \ |
f99be407 | 85 | } \ |
9dec086e | 86 | static inline int \ |
7083f0fe | 87 | __register_trace_##provider##___##name(void (*probe)(data_proto), void *data) \ |
f99be407 | 88 | { \ |
7083f0fe | 89 | return __tracepoint_probe_register(#provider ":" #name, (void *) probe, \ |
9dec086e | 90 | data); \ |
f99be407 | 91 | } \ |
9dec086e | 92 | static inline int \ |
7083f0fe | 93 | __unregister_trace_##provider##___##name(void (*probe)(data_proto), void *data) \ |
f99be407 | 94 | { \ |
7083f0fe | 95 | return __tracepoint_probe_unregister(#provider ":" #name, (void *) probe, \ |
9dec086e | 96 | data); \ |
f99be407 PMF |
97 | } |
98 | ||
332b3a18 | 99 | /* |
81614639 | 100 | * The need for the _DECLARE_TRACEPOINT_NOARGS() is to handle the prototype |
377d33ed | 101 | * (void). "void" is a special value in a function prototype and can |
8d71973c | 102 | * not be combined with other arguments. Since the DECLARE_TRACEPOINT() |
377d33ed MD |
103 | * macro adds a data element at the beginning of the prototype, |
104 | * we need a way to differentiate "(void *data, proto)" from | |
105 | * "(void *data, void)". The second prototype is invalid. | |
106 | * | |
8d71973c | 107 | * DECLARE_TRACEPOINT_NOARGS() passes "void" as the tracepoint prototype |
b979b346 | 108 | * and "void *__tp_cb_data" as the callback prototype. |
377d33ed | 109 | * |
8d71973c | 110 | * DECLARE_TRACEPOINT() passes "proto" as the tracepoint protoype and |
b979b346 | 111 | * "void *__tp_cb_data, proto" as the callback prototype. |
377d33ed | 112 | */ |
7083f0fe MD |
113 | #define _DECLARE_TRACEPOINT_NOARGS(provider, name) \ |
114 | __DECLARE_TRACEPOINT(provider, name, void, , void *__tp_cb_data, __tp_cb_data) | |
377d33ed | 115 | |
7083f0fe MD |
116 | #define _DECLARE_TRACEPOINT(provider, name, proto, args) \ |
117 | __DECLARE_TRACEPOINT(provider, name, TP_PARAMS(proto), TP_PARAMS(args), \ | |
81614639 MD |
118 | TP_PARAMS(void *__tp_cb_data, proto), \ |
119 | TP_PARAMS(__tp_cb_data, args)) | |
9dec086e | 120 | |
f99be407 | 121 | /* |
81614639 MD |
122 | * __tracepoints_ptrs section is not const (read-only) to let the linker update |
123 | * the pointer, allowing PIC code. | |
f99be407 | 124 | */ |
7083f0fe MD |
125 | #define _DEFINE_TRACEPOINT(provider, name) \ |
126 | static const char __tpstrtab_##provider##___##name[] \ | |
127 | __attribute__((section("__tracepoints_strings"))) = \ | |
128 | #provider ":" #name; \ | |
129 | struct tracepoint __tracepoint_##provider##___##name \ | |
81614639 | 130 | __attribute__((section("__tracepoints"))) = \ |
7083f0fe MD |
131 | { __tpstrtab_##provider##___##name, 0, NULL }; \ |
132 | static struct tracepoint * __tracepoint_ptr_##provider##___##name \ | |
81614639 | 133 | __attribute__((used, section("__tracepoints_ptrs"))) = \ |
7083f0fe | 134 | &__tracepoint_##provider##___##name; |
f99be407 | 135 | |
f99be407 | 136 | |
7083f0fe MD |
137 | #define __register_tracepoint(provider, name, probe, data) \ |
138 | __register_trace_##provider##___##name(probe, data) | |
139 | #define __unregister_tracepoint(provider, name, probe, data) \ | |
140 | __unregister_trace_##provider##___##name(probe, data) | |
f99be407 | 141 | |
81614639 MD |
142 | /* |
143 | * Connect a probe to a tracepoint. | |
144 | * Internal API. | |
145 | */ | |
146 | extern | |
147 | int __tracepoint_probe_register(const char *name, void *probe, void *data); | |
f99be407 PMF |
148 | |
149 | /* | |
81614639 MD |
150 | * Disconnect a probe from a tracepoint. |
151 | * Internal API. | |
f99be407 | 152 | */ |
81614639 MD |
153 | extern |
154 | int __tracepoint_probe_unregister(const char *name, void *probe, void *data); | |
f99be407 | 155 | |
81614639 MD |
156 | extern |
157 | int tracepoint_register_lib(struct tracepoint * const *tracepoints_start, | |
158 | int tracepoints_count); | |
159 | extern | |
160 | int tracepoint_unregister_lib(struct tracepoint * const *tracepoints_start); | |
872037bb | 161 | |
6cdf29f8 MD |
162 | /* |
163 | * These weak symbols, the constructor, and destructor take care of | |
164 | * registering only _one_ instance of the tracepoints per shared-ojbect | |
165 | * (or for the whole main program). | |
6cdf29f8 MD |
166 | */ |
167 | extern struct tracepoint * const __start___tracepoints_ptrs[] | |
168 | __attribute__((weak, visibility("hidden"))); | |
169 | extern struct tracepoint * const __stop___tracepoints_ptrs[] | |
170 | __attribute__((weak, visibility("hidden"))); | |
171 | int __tracepoint_registered | |
172 | __attribute__((weak, visibility("hidden"))); | |
173 | ||
174 | static void __attribute__((constructor)) __tracepoints__init(void) | |
175 | { | |
176 | if (__tracepoint_registered++) | |
177 | return; | |
178 | tracepoint_register_lib(__start___tracepoints_ptrs, | |
179 | __stop___tracepoints_ptrs - | |
180 | __start___tracepoints_ptrs); | |
181 | } | |
182 | ||
183 | static void __attribute__((destructor)) __tracepoints__destroy(void) | |
184 | { | |
185 | if (--__tracepoint_registered) | |
186 | return; | |
187 | tracepoint_unregister_lib(__start___tracepoints_ptrs); | |
188 | } | |
22d72948 | 189 | |
8d71973c | 190 | #ifndef TRACEPOINT_EVENT |
22d72948 | 191 | /* |
d75775a6 MD |
192 | * Usage of the TRACEPOINT_EVENT macro: |
193 | * | |
194 | * In short, an example: | |
195 | * | |
7083f0fe | 196 | * TRACEPOINT_EVENT(< [com_company_]project[_component] >, < event >, |
d75775a6 MD |
197 | * TP_PROTO(int arg0, void *arg1, char *string, size_t strlen, |
198 | * long *arg4, size_t arg4_len), | |
199 | * TP_ARGS(arg0, arg1, string, strlen, arg4, arg4_len), | |
200 | * TP_FIELDS( | |
201 | * | |
202 | * * Integer, printed in base 10 * | |
203 | * ctf_integer(int, field_a, arg0) | |
204 | * | |
205 | * * Integer, printed with 0x base 16 * | |
206 | * ctf_integer_hex(unsigned long, field_d, arg1) | |
207 | * | |
208 | * * Array Sequence, printed as UTF8-encoded array of bytes * | |
209 | * ctf_array_text(char, field_b, string, FIXED_LEN) | |
210 | * ctf_sequence_text(char, field_c, string, size_t, strlen) | |
211 | * | |
212 | * * String, printed as UTF8-encoded string * | |
213 | * ctf_string(field_e, string) | |
214 | * | |
215 | * * Array sequence of signed integer values * | |
216 | * ctf_array(long, field_f, arg4, FIXED_LEN4) | |
217 | * ctf_sequence(long, field_g, arg4, size_t, arg4_len) | |
218 | * ) | |
219 | * ) | |
220 | * | |
221 | * In more detail: | |
22d72948 | 222 | * |
0a42beb6 MD |
223 | * We define a tracepoint, its arguments, and its 'fast binary record' |
224 | * layout. | |
22d72948 | 225 | * |
7083f0fe | 226 | * Firstly, name your tracepoint via TRACEPOINT_EVENT(provider, name, |
22d72948 | 227 | * |
7083f0fe MD |
228 | * The provider and name should be a proper C99 identifier. |
229 | * The "provider" and "name" MUST follow these rules to ensure no | |
230 | * namespace clash occurs: | |
22d72948 | 231 | * |
0a42beb6 MD |
232 | * For projects (applications and libraries) for which an entity |
233 | * specific to the project controls the source code and thus its | |
234 | * tracepoints (typically with a scope larger than a single company): | |
22d72948 | 235 | * |
0a42beb6 | 236 | * either: |
7083f0fe | 237 | * project_component, event |
0a42beb6 | 238 | * or: |
7083f0fe | 239 | * project, event |
0a42beb6 MD |
240 | * |
241 | * Where "project" is the name of the project, | |
d5001b43 MD |
242 | * "component" is the name of the project component (which may |
243 | * include several levels of sub-components, e.g. | |
244 | * ...component_subcomponent_...) where the tracepoint is located | |
245 | * (optional), | |
0a42beb6 MD |
246 | * "event" is the name of the tracepoint event. |
247 | * | |
248 | * For projects issued from a single company wishing to advertise that | |
249 | * the company controls the source code and thus the tracepoints, the | |
250 | * "com_" prefix should be used: | |
251 | * | |
252 | * either: | |
7083f0fe | 253 | * com_company_project_component, event |
0a42beb6 | 254 | * or: |
7083f0fe | 255 | * com_company_project, event |
0a42beb6 MD |
256 | * |
257 | * Where "company" is the name of the company, | |
258 | * "project" is the name of the project, | |
d5001b43 MD |
259 | * "component" is the name of the project component (which may |
260 | * include several levels of sub-components, e.g. | |
261 | * ...component_subcomponent_...) where the tracepoint is located | |
262 | * (optional), | |
0a42beb6 MD |
263 | * "event" is the name of the tracepoint event. |
264 | * | |
b43de1e7 | 265 | * the provider:event identifier is limited to 127 characters. |
0a42beb6 MD |
266 | * |
267 | * As an example, let's consider a user-space application "someproject" | |
268 | * that would have an internal thread scheduler: | |
269 | * | |
7083f0fe | 270 | * TRACEPOINT_EVENT(someproject_sched, switch, |
22d72948 NC |
271 | * |
272 | * * | |
273 | * * A function has a regular function arguments | |
274 | * * prototype, declare it via TP_PROTO(): | |
275 | * * | |
276 | * | |
277 | * TP_PROTO(struct rq *rq, struct task_struct *prev, | |
278 | * struct task_struct *next), | |
279 | * | |
280 | * * | |
281 | * * Define the call signature of the 'function'. | |
282 | * * (Design sidenote: we use this instead of a | |
283 | * * TP_PROTO1/TP_PROTO2/TP_PROTO3 ugliness.) | |
284 | * * | |
285 | * | |
286 | * TP_ARGS(rq, prev, next), | |
287 | * | |
288 | * * | |
289 | * * Fast binary tracing: define the trace record via | |
81614639 | 290 | * * TP_FIELDS(). You can think about it like a |
22d72948 NC |
291 | * * regular C structure local variable definition. |
292 | * * | |
293 | * * This is how the trace record is structured and will | |
294 | * * be saved into the ring buffer. These are the fields | |
295 | * * that will be exposed to readers. | |
296 | * * | |
0a42beb6 | 297 | * * ctf_integer(pid_t, prev_pid, prev->pid) is equivalent |
81614639 | 298 | * * to a standard declaraton: |
22d72948 | 299 | * * |
81614639 | 300 | * * pid_t prev_pid; |
22d72948 | 301 | * * |
81614639 | 302 | * * followed by an assignment: |
22d72948 | 303 | * * |
81614639 | 304 | * * prev_pid = prev->pid; |
22d72948 | 305 | * * |
0a42beb6 | 306 | * * ctf_array(char, prev_comm, prev->comm, TASK_COMM_LEN) is |
81614639 | 307 | * * equivalent to: |
22d72948 | 308 | * * |
81614639 | 309 | * * char prev_comm[TASK_COMM_LEN]; |
22d72948 | 310 | * * |
81614639 | 311 | * * followed by an assignment: |
22d72948 | 312 | * * |
81614639 | 313 | * * memcpy(prev_comm, prev->comm, TASK_COMM_LEN); |
22d72948 NC |
314 | * * |
315 | * | |
81614639 | 316 | * TP_FIELDS( |
0a42beb6 MD |
317 | * ctf_array(char, prev_comm, prev->comm, TASK_COMM_LEN) |
318 | * ctf_integer(pid_t, prev_pid, prev->pid) | |
319 | * ctf_integer(int, prev_prio, prev->prio) | |
320 | * ctf_array(char, next_comm, next->comm, TASK_COMM_LEN) | |
321 | * ctf_integer(pid_t, next_pid, next->pid) | |
322 | * ctf_integer(int, next_prio, next->prio) | |
22d72948 | 323 | * ) |
0a42beb6 | 324 | * ) |
abcdecad MD |
325 | * |
326 | * Do _NOT_ add comma (,) nor semicolon (;) after the TRACEPOINT_EVENT | |
327 | * declaration. | |
328 | * | |
329 | * The TRACEPOINT_SYSTEM must be defined when declaring a | |
330 | * TRACEPOINT_EVENT. See ust/tracepoint-event.h for information about | |
331 | * usage of other macros controlling TRACEPOINT_EVENT. | |
22d72948 NC |
332 | */ |
333 | ||
7083f0fe MD |
334 | #define TRACEPOINT_EVENT(provider, name, proto, args, fields) \ |
335 | _DECLARE_TRACEPOINT(provider, name, TP_PARAMS(proto), TP_PARAMS(args)) | |
0c0686ee | 336 | |
7083f0fe MD |
337 | #define TRACEPOINT_EVENT_CLASS(provider, name, proto, args, fields) |
338 | #define TRACEPOINT_EVENT_INSTANCE(provider, template, name, proto, args)\ | |
339 | _DECLARE_TRACEPOINT(provider, name, TP_PARAMS(proto), TP_PARAMS(args)) | |
0c0686ee | 340 | |
81614639 MD |
341 | /* |
342 | * Declaration of tracepoints that take 0 argument. | |
343 | */ | |
7083f0fe MD |
344 | #define TRACEPOINT_EVENT_NOARGS(provider, name, fields) \ |
345 | _DECLARE_TRACEPOINT_NOARGS(provider, name) | |
0c0686ee | 346 | |
7083f0fe MD |
347 | #define TRACEPOINT_EVENT_CLASS_NOARGS(provider, name, fields) |
348 | #define TRACEPOINT_EVENT_INSTANCE_NOARGS(provider, template, name) \ | |
349 | _DECLARE_TRACEPOINT_NOARGS(provider, name) | |
0c0686ee | 350 | |
81614639 | 351 | #endif /* #ifndef TRACEPOINT_EVENT */ |
22d72948 | 352 | |
abcdecad MD |
353 | #ifndef TRACEPOINT_LOGLEVEL |
354 | ||
355 | /* | |
356 | * Tracepoint Loglevel Declaration Facility | |
357 | * | |
358 | * This is a place-holder the tracepoint loglevel declaration, | |
359 | * overridden by the tracer implementation. | |
360 | * | |
361 | * Typical use of these loglevels: | |
362 | * | |
363 | * 1) Declare the mapping between loglevel names and an integer values | |
364 | * within TRACEPOINT_LOGLEVEL_ENUM, using TP_LOGLEVEL for each tuple. | |
365 | * Do _NOT_ add comma (,) nor semicolon (;) between the | |
366 | * TRACEPOINT_LOGLEVEL_ENUM entries. Do _NOT_ add comma (,) nor | |
367 | * semicolon (;) after the TRACEPOINT_LOGLEVEL_ENUM declaration. The | |
368 | * name should be a proper C99 identifier. | |
369 | * | |
370 | * TRACEPOINT_LOGLEVEL_ENUM( | |
371 | * TP_LOGLEVEL( < loglevel_name >, < value > ) | |
372 | * TP_LOGLEVEL( < loglevel_name >, < value > ) | |
373 | * ... | |
374 | * ) | |
375 | * | |
376 | * e.g.: | |
377 | * | |
378 | * TRACEPOINT_LOGLEVEL_ENUM( | |
379 | * TP_LOGLEVEL(LOG_EMERG, 0) | |
380 | * TP_LOGLEVEL(LOG_ALERT, 1) | |
381 | * TP_LOGLEVEL(LOG_CRIT, 2) | |
382 | * TP_LOGLEVEL(LOG_ERR, 3) | |
383 | * TP_LOGLEVEL(LOG_WARNING, 4) | |
384 | * TP_LOGLEVEL(LOG_NOTICE, 5) | |
385 | * TP_LOGLEVEL(LOG_INFO, 6) | |
386 | * TP_LOGLEVEL(LOG_DEBUG, 7) | |
387 | * ) | |
388 | * | |
e9f37b4c MD |
389 | * 2) Then, declare tracepoint loglevels for tracepoints. A |
390 | * TRACEPOINT_EVENT should be declared prior to the the | |
391 | * TRACEPOINT_LOGLEVEL for a given tracepoint name. The first field | |
392 | * is the name of the tracepoint, the second field is the loglevel | |
393 | * name. | |
abcdecad | 394 | * |
7083f0fe | 395 | * TRACEPOINT_LOGLEVEL(< [com_company_]project[_component] >, < event >, |
abcdecad MD |
396 | * < loglevel_name >) |
397 | * | |
398 | * The TRACEPOINT_SYSTEM must be defined when declaring a | |
399 | * TRACEPOINT_LOGLEVEL_ENUM and TRACEPOINT_LOGLEVEL. The tracepoint | |
400 | * loglevel enumeration apply to the entire TRACEPOINT_SYSTEM. Only one | |
401 | * tracepoint loglevel enumeration should be declared per tracepoint | |
402 | * system. | |
403 | */ | |
404 | ||
405 | #define TRACEPOINT_LOGLEVEL_ENUM() | |
406 | #define TRACEPOINT_LOGLEVEL(name, loglevel) | |
407 | ||
408 | #endif /* #ifndef TRACEPOINT_LOGLEVEL */ | |
409 | ||
1ea2c2df MD |
410 | #ifdef __cplusplus |
411 | } | |
412 | #endif | |
413 | ||
51489cad | 414 | #endif /* _LTTNG_TRACEPOINT_H */ |