Use caa_unlikely, depend on liburcu >= 0.6.6
[ust.git] / include / ust / tracepoint.h
CommitLineData
27b052e3
PMF
1#ifndef _UST_TRACEPOINT_H
2#define _UST_TRACEPOINT_H
f99be407
PMF
3
4/*
1f8b0dff
PMF
5 * Copyright (C) 2008 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
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.
24 *
1f8b0dff 25 * Ported to userspace by Pierre-Marc Fournier.
f99be407
PMF
26 */
27
b7ea1a1c 28#include <urcu-bp.h>
b0c4126f 29#include <urcu/list.h>
5296e06c 30#include <urcu/compiler.h>
f99be407 31
b979b346 32struct tracepoint_probe {
9dec086e
NC
33 void *func;
34 void *data;
35};
36
f99be407
PMF
37struct tracepoint {
38 const char *name; /* Tracepoint name */
f36c12ab 39 char state; /* State. */
b979b346 40 struct tracepoint_probe *probes;
f218ff28 41};
f99be407 42
cc7b66ba
MD
43/*
44 * Tracepoints should be added to the instrumented code using the
45 * "tracepoint()" macro.
46 */
47#define tracepoint(name, args...) __trace_##name(args)
48
81614639
MD
49/*
50 * Library should be made known to libust by declaring TRACEPOINT_LIB in
51 * the source file. (Usually at the end of the file, in the outermost
52 * scope).
53 */
54#define TRACEPOINT_LIB \
55 extern struct tracepoint * const __start___tracepoints_ptrs[] __attribute__((weak, visibility("hidden"))); \
56 extern struct tracepoint * const __stop___tracepoints_ptrs[] __attribute__((weak, visibility("hidden"))); \
57 static struct tracepoint * __tracepoint_ptr_dummy \
58 __attribute__((used, section("__tracepoints_ptrs"))); \
59 static void __attribute__((constructor)) __tracepoints__init(void) \
60 { \
61 tracepoint_register_lib(__start___tracepoints_ptrs, \
62 __stop___tracepoints_ptrs - \
63 __start___tracepoints_ptrs); \
64 } \
65 \
66 static void __attribute__((destructor)) __tracepoints__destroy(void) \
67 { \
68 tracepoint_unregister_lib(__start___tracepoints_ptrs); \
69 }
f99be407
PMF
70
71/*
72 * it_func[0] is never NULL because there is at least one element in the array
73 * when the array itself is non NULL.
34dca3cc 74 * __attribute__((unused)) is for backward compatibility API.
f99be407
PMF
75 */
76#define __DO_TRACE(tp, proto, args) \
77 do { \
b979b346
MD
78 struct tracepoint_probe *__tp_it_probe_ptr; \
79 void *__tp_it_func; \
34dca3cc 80 void *__tp_cb_data __attribute__((unused)); \
f99be407 81 \
9dec086e 82 rcu_read_lock(); \
b979b346
MD
83 __tp_it_probe_ptr = rcu_dereference((tp)->probes); \
84 if (__tp_it_probe_ptr) { \
f99be407 85 do { \
b979b346
MD
86 __tp_it_func = __tp_it_probe_ptr->func; \
87 __tp_cb_data = __tp_it_probe_ptr->data; \
88 ((void(*)(proto))__tp_it_func)(args); \
89 } while ((++__tp_it_probe_ptr)->func); \
f99be407 90 } \
9dec086e 91 rcu_read_unlock(); \
f99be407
PMF
92 } while (0)
93
81614639
MD
94#define TP_PARAMS(args...) args
95#define TP_PROTO(args...) args
96#define TP_ARGS(args...) args
97
f36c12ab 98#define __CHECK_TRACE(name, proto, args) \
f99be407 99 do { \
5296e06c 100 if (caa_unlikely(__tracepoint_##name.state)) \
f36c12ab
MD
101 __DO_TRACE(&__tracepoint_##name, \
102 TP_PROTO(proto), TP_ARGS(args)); \
f99be407
PMF
103 } while (0)
104
105/*
106 * Make sure the alignment of the structure in the __tracepoints section will
107 * not add unwanted padding between the beginning of the section and the
108 * structure. Force alignment to the same alignment as the section start.
f99be407 109 */
8d71973c 110#define __DECLARE_TRACEPOINT(name, proto, args, data_proto, data_args) \
f99be407 111 extern struct tracepoint __tracepoint_##name; \
cc7b66ba 112 static inline void __trace_##name(proto) \
f99be407 113 { \
f36c12ab 114 __CHECK_TRACE(name, TP_PROTO(data_proto), \
9dec086e 115 TP_ARGS(data_args)); \
f99be407 116 } \
9dec086e 117 static inline int \
cc7b66ba 118 __register_trace_##name(void (*probe)(data_proto), void *data) \
f99be407 119 { \
81614639 120 return __tracepoint_probe_register(#name, (void *)probe,\
9dec086e
NC
121 data); \
122 \
f99be407 123 } \
9dec086e 124 static inline int \
cc7b66ba 125 __unregister_trace_##name(void (*probe)(data_proto), void *data)\
f99be407 126 { \
81614639 127 return __tracepoint_probe_unregister(#name, (void *)probe, \
9dec086e 128 data); \
2874fee5
MD
129 } \
130 /* \
131 * Backward-compatibility API (will be deprecated): \
132 * trace_* \
133 * register_trace_* \
134 * unregister_trace_* \
135 */ \
136 static inline void trace_##name(proto) \
137 { \
138 __CHECK_TRACE(name, TP_PROTO(proto), \
139 TP_ARGS(args)); \
140 } \
141 static inline int \
142 register_trace_##name(void (*probe)(proto)) \
143 { \
144 return __tracepoint_probe_register(#name, (void *)probe,\
145 NULL); \
146 } \
147 static inline int \
148 unregister_trace_##name(void (*probe)(proto)) \
149 { \
150 return __tracepoint_probe_unregister(#name, (void *)probe, \
151 NULL); \
f99be407
PMF
152 }
153
332b3a18 154/*
81614639 155 * The need for the _DECLARE_TRACEPOINT_NOARGS() is to handle the prototype
377d33ed 156 * (void). "void" is a special value in a function prototype and can
8d71973c 157 * not be combined with other arguments. Since the DECLARE_TRACEPOINT()
377d33ed
MD
158 * macro adds a data element at the beginning of the prototype,
159 * we need a way to differentiate "(void *data, proto)" from
160 * "(void *data, void)". The second prototype is invalid.
161 *
8d71973c 162 * DECLARE_TRACEPOINT_NOARGS() passes "void" as the tracepoint prototype
b979b346 163 * and "void *__tp_cb_data" as the callback prototype.
377d33ed 164 *
8d71973c 165 * DECLARE_TRACEPOINT() passes "proto" as the tracepoint protoype and
b979b346 166 * "void *__tp_cb_data, proto" as the callback prototype.
377d33ed 167 */
81614639
MD
168#define _DECLARE_TRACEPOINT_NOARGS(name) \
169 __DECLARE_TRACEPOINT(name, void, , void *__tp_cb_data, __tp_cb_data)
377d33ed 170
81614639
MD
171#define _DECLARE_TRACEPOINT(name, proto, args) \
172 __DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args), \
173 TP_PARAMS(void *__tp_cb_data, proto), \
174 TP_PARAMS(__tp_cb_data, args))
9dec086e 175
f99be407 176/*
81614639
MD
177 * __tracepoints_ptrs section is not const (read-only) to let the linker update
178 * the pointer, allowing PIC code.
f99be407 179 */
81614639
MD
180#define _DEFINE_TRACEPOINT(name) \
181 static const char __tpstrtab_##name[] \
182 __attribute__((section("__tracepoints_strings"))) = #name; \
183 struct tracepoint __tracepoint_##name \
184 __attribute__((section("__tracepoints"))) = \
185 { __tpstrtab_##name, 0, NULL }; \
186 static struct tracepoint * __tracepoint_ptr_##name \
187 __attribute__((used, section("__tracepoints_ptrs"))) = \
188 &__tracepoint_##name;
f99be407 189
f99be407 190
81614639
MD
191#define __register_tracepoint(name, probe, data) \
192 __register_trace_##name(probe, data)
193#define __unregister_tracepoint(name, probe, data) \
194 __unregister_trace_##name(probe, data)
f99be407 195
81614639
MD
196/*
197 * Connect a probe to a tracepoint.
198 * Internal API.
199 */
200extern
201int __tracepoint_probe_register(const char *name, void *probe, void *data);
f99be407
PMF
202
203/*
81614639
MD
204 * Disconnect a probe from a tracepoint.
205 * Internal API.
f99be407 206 */
81614639
MD
207extern
208int __tracepoint_probe_unregister(const char *name, void *probe, void *data);
f99be407 209
474d745f 210struct tracepoint_lib {
f218ff28 211 struct tracepoint * const *tracepoints_start;
474d745f 212 int tracepoints_count;
0222e121 213 struct cds_list_head list;
474d745f
PMF
214};
215
81614639
MD
216extern
217int tracepoint_register_lib(struct tracepoint * const *tracepoints_start,
218 int tracepoints_count);
219extern
220int tracepoint_unregister_lib(struct tracepoint * const *tracepoints_start);
872037bb 221
2874fee5
MD
222/*
223 * Backward-compatibility API (will be deprecated):
224 * DEFINE_TRACE
225 * DECLARE_TRACE
226 * register_tracepoint
227 * unregister_tracepoint
228 */
229#define DEFINE_TRACE _DEFINE_TRACEPOINT
230#define DECLARE_TRACE(name, proto, args) \
231 _DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args))
22d72948 232
8d71973c 233#ifndef TRACEPOINT_EVENT
22d72948 234/*
8d71973c 235 * For use with the TRACEPOINT_EVENT macro:
22d72948
NC
236 *
237 * We define a tracepoint, its arguments, its printf format
238 * and its 'fast binary record' layout.
239 *
8d71973c 240 * Firstly, name your tracepoint via TRACEPOINT_EVENT(name : the
22d72948
NC
241 * 'subsystem_event' notation is fine.
242 *
243 * Think about this whole construct as the
244 * 'trace_sched_switch() function' from now on.
245 *
246 *
8d71973c 247 * TRACEPOINT_EVENT(sched_switch,
22d72948
NC
248 *
249 * *
250 * * A function has a regular function arguments
251 * * prototype, declare it via TP_PROTO():
252 * *
253 *
254 * TP_PROTO(struct rq *rq, struct task_struct *prev,
255 * struct task_struct *next),
256 *
257 * *
258 * * Define the call signature of the 'function'.
259 * * (Design sidenote: we use this instead of a
260 * * TP_PROTO1/TP_PROTO2/TP_PROTO3 ugliness.)
261 * *
262 *
263 * TP_ARGS(rq, prev, next),
264 *
265 * *
266 * * Fast binary tracing: define the trace record via
81614639 267 * * TP_FIELDS(). You can think about it like a
22d72948
NC
268 * * regular C structure local variable definition.
269 * *
270 * * This is how the trace record is structured and will
271 * * be saved into the ring buffer. These are the fields
272 * * that will be exposed to readers.
273 * *
81614639
MD
274 * * tp_field(pid_t, prev_pid, prev->pid) is equivalent
275 * * to a standard declaraton:
22d72948 276 * *
81614639 277 * * pid_t prev_pid;
22d72948 278 * *
81614639 279 * * followed by an assignment:
22d72948 280 * *
81614639 281 * * prev_pid = prev->pid;
22d72948 282 * *
81614639
MD
283 * * tp_array(char, prev_comm, TASK_COMM_LEN, prev->comm) is
284 * * equivalent to:
22d72948 285 * *
81614639 286 * * char prev_comm[TASK_COMM_LEN];
22d72948 287 * *
81614639 288 * * followed by an assignment:
22d72948 289 * *
81614639 290 * * memcpy(prev_comm, prev->comm, TASK_COMM_LEN);
22d72948
NC
291 * *
292 *
81614639
MD
293 * TP_FIELDS(
294 * tp_array(char, prev_comm, TASK_COMM_LEN, prev->comm)
295 * tp_field(pid_t, prev_pid, prev->pid)
296 * tp_field(int, prev_prio, prev->prio)
297 * tp_array(char, next_comm, TASK_COMM_LEN, next->comm)
298 * tp_field(pid_t, next_pid, next->pid)
299 * tp_field(int, next_prio, next->prio)
22d72948 300 * )
22d72948 301 * );
22d72948
NC
302 */
303
81614639
MD
304#define TRACEPOINT_EVENT(name, proto, args, fields) \
305 _DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args))
0c0686ee 306
81614639
MD
307#define TRACEPOINT_EVENT_CLASS(name, proto, args, fields)
308#define TRACEPOINT_EVENT_INSTANCE(template, name, proto, args) \
309 _DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args))
0c0686ee 310
81614639
MD
311/*
312 * Declaration of tracepoints that take 0 argument.
313 */
314#define TRACEPOINT_EVENT_NOARGS(name, fields) \
315 _DECLARE_TRACEPOINT_NOARGS(name)
0c0686ee 316
81614639
MD
317#define TRACEPOINT_EVENT_CLASS_NOARGS(name, fields)
318#define TRACEPOINT_EVENT_INSTANCE_NOARGS(template, name) \
319 _DECLARE_TRACEPOINT_NOARGS(name)
0c0686ee 320
0c0686ee 321
0c0686ee 322
8d71973c 323#define TRACEPOINT_EVENT_LIB \
fc1caebc 324 extern struct trace_event * const __start___trace_events_ptrs[] \
0c0686ee 325 __attribute__((weak, visibility("hidden"))); \
fc1caebc 326 extern struct trace_event * const __stop___trace_events_ptrs[] \
0c0686ee 327 __attribute__((weak, visibility("hidden"))); \
332b3a18
MD
328 static struct trace_event * __event_ptrs_dummy \
329 __attribute__((used, section("__trace_events_ptrs"))); \
0c0686ee
NC
330 static void __attribute__((constructor)) \
331 __trace_events__init(void) \
332 { \
fc1caebc
MD
333 trace_event_register_lib(__start___trace_events_ptrs, \
334 __stop___trace_events_ptrs - \
335 __start___trace_events_ptrs); \
0c0686ee
NC
336 } \
337 \
338 static void __attribute__((destructor)) \
339 __trace_event__destroy(void) \
340 { \
fc1caebc 341 trace_event_unregister_lib(__start___trace_events_ptrs);\
0c0686ee
NC
342 }
343
81614639
MD
344struct trace_event {
345 const char *name;
346};
22d72948 347
81614639
MD
348struct trace_event_lib {
349 struct trace_event * const *trace_events_start;
350 int trace_events_count;
351 struct cds_list_head list;
352};
22d72948 353
81614639
MD
354extern
355int trace_event_register_lib(struct trace_event * const *start_trace_events,
356 int trace_event_count);
357extern
358int trace_event_unregister_lib(struct trace_event * const *start_trace_events);
22d72948 359
81614639 360#endif /* #ifndef TRACEPOINT_EVENT */
22d72948 361
27b052e3 362#endif /* _UST_TRACEPOINT_H */
This page took 0.056159 seconds and 4 git commands to generate.