f4d7d6091b3116c7323b3c4dbebe5c8047a678bc
[lttng-ust.git] / include / lttng / tracepoint.h
1 #ifndef _LTTNG_TRACEPOINT_H
2 #define _LTTNG_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
26 #include <urcu-bp.h>
27 #include <urcu/list.h>
28 #include <lttng/tracepoint-types.h>
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 /*
35 * Tracepoints should be added to the instrumented code using the
36 * "tracepoint()" macro.
37 */
38 #define tracepoint(provider, name, args...) \
39 __trace_##provider##___##name(args)
40
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 { \
47 struct tracepoint_probe *__tp_it_probe_ptr; \
48 void *__tp_it_func; \
49 void *__tp_cb_data; \
50 \
51 rcu_read_lock(); \
52 __tp_it_probe_ptr = rcu_dereference((tp)->probes); \
53 if (__tp_it_probe_ptr) { \
54 do { \
55 __tp_it_func = __tp_it_probe_ptr->func; \
56 __tp_cb_data = __tp_it_probe_ptr->data; \
57 URCU_FORCE_CAST(void(*)(proto), __tp_it_func)(args); \
58 } while ((++__tp_it_probe_ptr)->func); \
59 } \
60 rcu_read_unlock(); \
61 } while (0)
62
63 #define TP_PARAMS(args...) args
64 #define TP_PROTO(args...) args
65 #define TP_VARS(args...) args
66
67 #define __CHECK_TRACE(provider, name, proto, args) \
68 do { \
69 if (caa_unlikely(__tracepoint_##provider##___##name.state)) \
70 __DO_TRACE(&__tracepoint_##provider##___##name, \
71 TP_PROTO(proto), TP_VARS(args)); \
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.
78 */
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) \
82 { \
83 __CHECK_TRACE(provider, name, TP_PROTO(data_proto), \
84 TP_VARS(data_args)); \
85 } \
86 static inline int \
87 __register_trace_##provider##___##name(void (*probe)(data_proto), void *data) \
88 { \
89 return __tracepoint_probe_register(#provider ":" #name, (void *) probe, \
90 data); \
91 } \
92 static inline int \
93 __unregister_trace_##provider##___##name(void (*probe)(data_proto), void *data) \
94 { \
95 return __tracepoint_probe_unregister(#provider ":" #name, (void *) probe, \
96 data); \
97 }
98
99 /*
100 * The need for the _DECLARE_TRACEPOINT_NOARGS() is to handle the prototype
101 * (void). "void" is a special value in a function prototype and can
102 * not be combined with other arguments. Since the DECLARE_TRACEPOINT()
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 *
107 * DECLARE_TRACEPOINT_NOARGS() passes "void" as the tracepoint prototype
108 * and "void *__tp_cb_data" as the callback prototype.
109 *
110 * DECLARE_TRACEPOINT() passes "proto" as the tracepoint protoype and
111 * "void *__tp_cb_data, proto" as the callback prototype.
112 */
113 #define _DECLARE_TRACEPOINT_NOARGS(provider, name) \
114 __DECLARE_TRACEPOINT(provider, name, void, , void *__tp_cb_data, __tp_cb_data)
115
116 #define _DECLARE_TRACEPOINT(provider, name, proto, args) \
117 __DECLARE_TRACEPOINT(provider, name, TP_PARAMS(proto), TP_PARAMS(args), \
118 TP_PARAMS(void *__tp_cb_data, proto), \
119 TP_PARAMS(__tp_cb_data, args))
120
121 /*
122 * __tracepoints_ptrs section is not const (read-only) to let the linker update
123 * the pointer, allowing PIC code.
124 */
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 \
130 __attribute__((section("__tracepoints"))) = \
131 { __tpstrtab_##provider##___##name, 0, NULL }; \
132 static struct tracepoint * __tracepoint_ptr_##provider##___##name \
133 __attribute__((used, section("__tracepoints_ptrs"))) = \
134 &__tracepoint_##provider##___##name;
135
136
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)
141
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);
148
149 /*
150 * Disconnect a probe from a tracepoint.
151 * Internal API.
152 */
153 extern
154 int __tracepoint_probe_unregister(const char *name, void *probe, void *data);
155
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);
161
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).
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 }
189
190 #ifndef TRACEPOINT_EVENT
191 /*
192 * Usage of the TRACEPOINT_EVENT macro:
193 *
194 * In short, an example:
195 *
196 * TRACEPOINT_EVENT(< [com_company_]project[_component] >, < event >,
197 * TP_PROTO(int arg0, void *arg1, char *string, size_t strlen,
198 * long *arg4, size_t arg4_len),
199 * TP_VARS(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:
222 *
223 * We define a tracepoint, its arguments, and its 'fast binary record'
224 * layout.
225 *
226 * Firstly, name your tracepoint via TRACEPOINT_EVENT(provider, name,
227 *
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:
231 *
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):
235 *
236 * either:
237 * project_component, event
238 * or:
239 * project, event
240 *
241 * Where "project" is the name of the project,
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),
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:
253 * com_company_project_component, event
254 * or:
255 * com_company_project, event
256 *
257 * Where "company" is the name of the company,
258 * "project" is the name of the project,
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),
263 * "event" is the name of the tracepoint event.
264 *
265 * the provider:event identifier is limited to 127 characters.
266 *
267 * As an example, let's consider a user-space application "someproject"
268 * that would have an internal thread scheduler:
269 *
270 * TRACEPOINT_EVENT(someproject_sched, switch,
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_VARS(rq, prev, next),
287 *
288 * *
289 * * Fast binary tracing: define the trace record via
290 * * TP_FIELDS(). You can think about it like a
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 * *
297 * * ctf_integer(pid_t, prev_pid, prev->pid) is equivalent
298 * * to a standard declaraton:
299 * *
300 * * pid_t prev_pid;
301 * *
302 * * followed by an assignment:
303 * *
304 * * prev_pid = prev->pid;
305 * *
306 * * ctf_array(char, prev_comm, prev->comm, TASK_COMM_LEN) is
307 * * equivalent to:
308 * *
309 * * char prev_comm[TASK_COMM_LEN];
310 * *
311 * * followed by an assignment:
312 * *
313 * * memcpy(prev_comm, prev->comm, TASK_COMM_LEN);
314 * *
315 *
316 * TP_FIELDS(
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)
323 * )
324 * )
325 *
326 * Do _NOT_ add comma (,) nor semicolon (;) after the TRACEPOINT_EVENT
327 * declaration.
328 *
329 * The TRACEPOINT_PROVIDER 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.
332 */
333
334 #define TRACEPOINT_EVENT(provider, name, proto, args, fields) \
335 _DECLARE_TRACEPOINT(provider, name, TP_PARAMS(proto), TP_PARAMS(args))
336
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))
340
341 /*
342 * Declaration of tracepoints that take 0 argument.
343 */
344 #define TRACEPOINT_EVENT_NOARGS(provider, name, fields) \
345 _DECLARE_TRACEPOINT_NOARGS(provider, name)
346
347 #define TRACEPOINT_EVENT_CLASS_NOARGS(provider, name, fields)
348 #define TRACEPOINT_EVENT_INSTANCE_NOARGS(provider, template, name) \
349 _DECLARE_TRACEPOINT_NOARGS(provider, name)
350
351 #endif /* #ifndef TRACEPOINT_EVENT */
352
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 *
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.
394 *
395 * TRACEPOINT_LOGLEVEL(< [com_company_]project[_component] >, < event >,
396 * < loglevel_name >)
397 *
398 * The TRACEPOINT_PROVIDER must be defined when declaring a
399 * TRACEPOINT_LOGLEVEL_ENUM and TRACEPOINT_LOGLEVEL. The tracepoint
400 * loglevel enumeration apply to the entire TRACEPOINT_PROVIDER. Only one
401 * tracepoint loglevel enumeration should be declared per tracepoint
402 * provider.
403 */
404
405 #define TRACEPOINT_LOGLEVEL_ENUM()
406 #define TRACEPOINT_LOGLEVEL(name, loglevel)
407
408 #endif /* #ifndef TRACEPOINT_LOGLEVEL */
409
410 #ifdef __cplusplus
411 }
412 #endif
413
414 #endif /* _LTTNG_TRACEPOINT_H */
This page took 0.060799 seconds and 4 git commands to generate.