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