1 #ifndef _UST_TRACEPOINT_H
2 #define _UST_TRACEPOINT_H
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>
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.
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.
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
23 * Heavily inspired from the Linux Kernel Markers.
25 * Ported to userspace by Pierre-Marc Fournier.
29 #include <urcu/list.h>
31 struct tracepoint_probe
{
37 const char *name
; /* Tracepoint name */
38 char state
; /* State. */
39 struct tracepoint_probe
*probes
;
43 * Tracepoints should be added to the instrumented code using the
44 * "tracepoint()" macro.
46 #define tracepoint(name, args...) __trace_##name(args)
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.
52 #define __DO_TRACE(tp, proto, args) \
54 struct tracepoint_probe *__tp_it_probe_ptr; \
59 __tp_it_probe_ptr = rcu_dereference((tp)->probes); \
60 if (__tp_it_probe_ptr) { \
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); \
70 #define TP_PARAMS(args...) args
71 #define TP_PROTO(args...) args
72 #define TP_ARGS(args...) args
74 #define __CHECK_TRACE(name, proto, args) \
76 if (unlikely(__tracepoint_##name.state)) \
77 __DO_TRACE(&__tracepoint_##name, \
78 TP_PROTO(proto), TP_ARGS(args)); \
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.
86 #define __DECLARE_TRACEPOINT(name, proto, args, data_proto, data_args) \
87 extern struct tracepoint __tracepoint_##name; \
88 static inline void __trace_##name(proto) \
90 __CHECK_TRACE(name, TP_PROTO(data_proto), \
91 TP_ARGS(data_args)); \
94 __register_trace_##name(void (*probe)(data_proto), void *data) \
96 return __tracepoint_probe_register(#name, (void *)probe,\
101 __unregister_trace_##name(void (*probe)(data_proto), void *data)\
103 return __tracepoint_probe_unregister(#name, (void *)probe, \
108 * The need for the _DECLARE_TRACEPOINT_NOARGS() is to handle the prototype
109 * (void). "void" is a special value in a function prototype and can
110 * not be combined with other arguments. Since the DECLARE_TRACEPOINT()
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.
115 * DECLARE_TRACEPOINT_NOARGS() passes "void" as the tracepoint prototype
116 * and "void *__tp_cb_data" as the callback prototype.
118 * DECLARE_TRACEPOINT() passes "proto" as the tracepoint protoype and
119 * "void *__tp_cb_data, proto" as the callback prototype.
121 #define _DECLARE_TRACEPOINT_NOARGS(name) \
122 __DECLARE_TRACEPOINT(name, void, , void *__tp_cb_data, __tp_cb_data)
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))
130 * __tracepoints_ptrs section is not const (read-only) to let the linker update
131 * the pointer, allowing PIC code.
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;
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)
150 * Connect a probe to a tracepoint.
154 int __tracepoint_probe_register(const char *name
, void *probe
, void *data
);
157 * Disconnect a probe from a tracepoint.
161 int __tracepoint_probe_unregister(const char *name
, void *probe
, void *data
);
164 int tracepoint_register_lib(struct tracepoint
* const *tracepoints_start
,
165 int tracepoints_count
);
167 int tracepoint_unregister_lib(struct tracepoint
* const *tracepoints_start
);
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).
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")));
181 static void __attribute__((constructor
)) __tracepoints__init(void)
183 if (__tracepoint_registered
++)
185 tracepoint_register_lib(__start___tracepoints_ptrs
,
186 __stop___tracepoints_ptrs
-
187 __start___tracepoints_ptrs
);
190 static void __attribute__((destructor
)) __tracepoints__destroy(void)
192 if (--__tracepoint_registered
)
194 tracepoint_unregister_lib(__start___tracepoints_ptrs
);
197 #ifndef TRACEPOINT_EVENT
199 * Usage of the TRACEPOINT_EVENT macro:
201 * In short, an example:
203 * TRACEPOINT_EVENT(< [com_company_]project_[component_]event >,
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),
209 * * Integer, printed in base 10 *
210 * ctf_integer(int, field_a, arg0)
212 * * Integer, printed with 0x base 16 *
213 * ctf_integer_hex(unsigned long, field_d, arg1)
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)
219 * * String, printed as UTF8-encoded string *
220 * ctf_string(field_e, string)
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)
230 * We define a tracepoint, its arguments, and its 'fast binary record'
233 * Firstly, name your tracepoint via TRACEPOINT_EVENT(name,
235 * The name should be a proper C99 identifier.
236 * The "name" MUST follow these rules to ensure no namespace clash
239 * For projects (applications and libraries) for which an entity
240 * specific to the project controls the source code and thus its
241 * tracepoints (typically with a scope larger than a single company):
244 * project_component_event
248 * Where "project" is the name of the project,
249 * "component" is the name of the project component (which may
250 * include several levels of sub-components, e.g.
251 * ...component_subcomponent_...) where the tracepoint is located
253 * "event" is the name of the tracepoint event.
255 * For projects issued from a single company wishing to advertise that
256 * the company controls the source code and thus the tracepoints, the
257 * "com_" prefix should be used:
260 * com_company_project_component_event
262 * com_company_project_event
264 * Where "company" is the name of the company,
265 * "project" is the name of the project,
266 * "component" is the name of the project component (which may
267 * include several levels of sub-components, e.g.
268 * ...component_subcomponent_...) where the tracepoint is located
270 * "event" is the name of the tracepoint event.
273 * As an example, let's consider a user-space application "someproject"
274 * that would have an internal thread scheduler:
276 * TRACEPOINT_EVENT(someproject_sched_switch,
279 * * A function has a regular function arguments
280 * * prototype, declare it via TP_PROTO():
283 * TP_PROTO(struct rq *rq, struct task_struct *prev,
284 * struct task_struct *next),
287 * * Define the call signature of the 'function'.
288 * * (Design sidenote: we use this instead of a
289 * * TP_PROTO1/TP_PROTO2/TP_PROTO3 ugliness.)
292 * TP_ARGS(rq, prev, next),
295 * * Fast binary tracing: define the trace record via
296 * * TP_FIELDS(). You can think about it like a
297 * * regular C structure local variable definition.
299 * * This is how the trace record is structured and will
300 * * be saved into the ring buffer. These are the fields
301 * * that will be exposed to readers.
303 * * ctf_integer(pid_t, prev_pid, prev->pid) is equivalent
304 * * to a standard declaraton:
308 * * followed by an assignment:
310 * * prev_pid = prev->pid;
312 * * ctf_array(char, prev_comm, prev->comm, TASK_COMM_LEN) is
315 * * char prev_comm[TASK_COMM_LEN];
317 * * followed by an assignment:
319 * * memcpy(prev_comm, prev->comm, TASK_COMM_LEN);
323 * ctf_array(char, prev_comm, prev->comm, TASK_COMM_LEN)
324 * ctf_integer(pid_t, prev_pid, prev->pid)
325 * ctf_integer(int, prev_prio, prev->prio)
326 * ctf_array(char, next_comm, next->comm, TASK_COMM_LEN)
327 * ctf_integer(pid_t, next_pid, next->pid)
328 * ctf_integer(int, next_prio, next->prio)
332 * Do _NOT_ add comma (,) nor semicolon (;) after the TRACEPOINT_EVENT
335 * The TRACEPOINT_SYSTEM must be defined when declaring a
336 * TRACEPOINT_EVENT. See ust/tracepoint-event.h for information about
337 * usage of other macros controlling TRACEPOINT_EVENT.
340 #define TRACEPOINT_EVENT(name, proto, args, fields) \
341 _DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args))
343 #define TRACEPOINT_EVENT_CLASS(name, proto, args, fields)
344 #define TRACEPOINT_EVENT_INSTANCE(template, name, proto, args) \
345 _DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args))
348 * Declaration of tracepoints that take 0 argument.
350 #define TRACEPOINT_EVENT_NOARGS(name, fields) \
351 _DECLARE_TRACEPOINT_NOARGS(name)
353 #define TRACEPOINT_EVENT_CLASS_NOARGS(name, fields)
354 #define TRACEPOINT_EVENT_INSTANCE_NOARGS(template, name) \
355 _DECLARE_TRACEPOINT_NOARGS(name)
357 #endif /* #ifndef TRACEPOINT_EVENT */
359 #ifndef TRACEPOINT_LOGLEVEL
362 * Tracepoint Loglevel Declaration Facility
364 * This is a place-holder the tracepoint loglevel declaration,
365 * overridden by the tracer implementation.
367 * Typical use of these loglevels:
369 * 1) Declare the mapping between loglevel names and an integer values
370 * within TRACEPOINT_LOGLEVEL_ENUM, using TP_LOGLEVEL for each tuple.
371 * Do _NOT_ add comma (,) nor semicolon (;) between the
372 * TRACEPOINT_LOGLEVEL_ENUM entries. Do _NOT_ add comma (,) nor
373 * semicolon (;) after the TRACEPOINT_LOGLEVEL_ENUM declaration. The
374 * name should be a proper C99 identifier.
376 * TRACEPOINT_LOGLEVEL_ENUM(
377 * TP_LOGLEVEL( < loglevel_name >, < value > )
378 * TP_LOGLEVEL( < loglevel_name >, < value > )
384 * TRACEPOINT_LOGLEVEL_ENUM(
385 * TP_LOGLEVEL(LOG_EMERG, 0)
386 * TP_LOGLEVEL(LOG_ALERT, 1)
387 * TP_LOGLEVEL(LOG_CRIT, 2)
388 * TP_LOGLEVEL(LOG_ERR, 3)
389 * TP_LOGLEVEL(LOG_WARNING, 4)
390 * TP_LOGLEVEL(LOG_NOTICE, 5)
391 * TP_LOGLEVEL(LOG_INFO, 6)
392 * TP_LOGLEVEL(LOG_DEBUG, 7)
395 * 2) Then, declare tracepoint loglevels for tracepoints. A
396 * TRACEPOINT_EVENT should be declared prior to the the
397 * TRACEPOINT_LOGLEVEL for a given tracepoint name. The first field
398 * is the name of the tracepoint, the second field is the loglevel
401 * TRACEPOINT_LOGLEVEL(< [com_company_]project_[component_]event >,
404 * The TRACEPOINT_SYSTEM must be defined when declaring a
405 * TRACEPOINT_LOGLEVEL_ENUM and TRACEPOINT_LOGLEVEL. The tracepoint
406 * loglevel enumeration apply to the entire TRACEPOINT_SYSTEM. Only one
407 * tracepoint loglevel enumeration should be declared per tracepoint
411 #define TRACEPOINT_LOGLEVEL_ENUM()
412 #define TRACEPOINT_LOGLEVEL(name, loglevel)
414 #endif /* #ifndef TRACEPOINT_LOGLEVEL */
416 #endif /* _UST_TRACEPOINT_H */