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