TRACEPOINT_EVENT API
[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>
f99be407 30
b979b346 31struct tracepoint_probe {
9dec086e
NC
32 void *func;
33 void *data;
34};
35
f99be407
PMF
36struct tracepoint {
37 const char *name; /* Tracepoint name */
f36c12ab 38 char state; /* State. */
b979b346 39 struct tracepoint_probe *probes;
f218ff28 40};
f99be407 41
cc7b66ba
MD
42/*
43 * Tracepoints should be added to the instrumented code using the
44 * "tracepoint()" macro.
45 */
46#define tracepoint(name, args...) __trace_##name(args)
47
81614639
MD
48/*
49 * Library should be made known to libust by declaring TRACEPOINT_LIB in
50 * the source file. (Usually at the end of the file, in the outermost
51 * scope).
52 */
53#define TRACEPOINT_LIB \
54 extern struct tracepoint * const __start___tracepoints_ptrs[] __attribute__((weak, visibility("hidden"))); \
55 extern struct tracepoint * const __stop___tracepoints_ptrs[] __attribute__((weak, visibility("hidden"))); \
56 static struct tracepoint * __tracepoint_ptr_dummy \
57 __attribute__((used, section("__tracepoints_ptrs"))); \
58 static void __attribute__((constructor)) __tracepoints__init(void) \
59 { \
60 tracepoint_register_lib(__start___tracepoints_ptrs, \
61 __stop___tracepoints_ptrs - \
62 __start___tracepoints_ptrs); \
63 } \
64 \
65 static void __attribute__((destructor)) __tracepoints__destroy(void) \
66 { \
67 tracepoint_unregister_lib(__start___tracepoints_ptrs); \
68 }
f99be407
PMF
69
70/*
71 * it_func[0] is never NULL because there is at least one element in the array
72 * when the array itself is non NULL.
73 */
74#define __DO_TRACE(tp, proto, args) \
75 do { \
b979b346
MD
76 struct tracepoint_probe *__tp_it_probe_ptr; \
77 void *__tp_it_func; \
78 void *__tp_cb_data; \
f99be407 79 \
9dec086e 80 rcu_read_lock(); \
b979b346
MD
81 __tp_it_probe_ptr = rcu_dereference((tp)->probes); \
82 if (__tp_it_probe_ptr) { \
f99be407 83 do { \
b979b346
MD
84 __tp_it_func = __tp_it_probe_ptr->func; \
85 __tp_cb_data = __tp_it_probe_ptr->data; \
86 ((void(*)(proto))__tp_it_func)(args); \
87 } while ((++__tp_it_probe_ptr)->func); \
f99be407 88 } \
9dec086e 89 rcu_read_unlock(); \
f99be407
PMF
90 } while (0)
91
81614639
MD
92#define TP_PARAMS(args...) args
93#define TP_PROTO(args...) args
94#define TP_ARGS(args...) args
95
f36c12ab 96#define __CHECK_TRACE(name, proto, args) \
f99be407 97 do { \
f36c12ab
MD
98 if (unlikely(__tracepoint_##name.state)) \
99 __DO_TRACE(&__tracepoint_##name, \
100 TP_PROTO(proto), TP_ARGS(args)); \
f99be407
PMF
101 } while (0)
102
103/*
104 * Make sure the alignment of the structure in the __tracepoints section will
105 * not add unwanted padding between the beginning of the section and the
106 * structure. Force alignment to the same alignment as the section start.
f99be407 107 */
8d71973c 108#define __DECLARE_TRACEPOINT(name, proto, args, data_proto, data_args) \
f99be407 109 extern struct tracepoint __tracepoint_##name; \
cc7b66ba 110 static inline void __trace_##name(proto) \
f99be407 111 { \
f36c12ab 112 __CHECK_TRACE(name, TP_PROTO(data_proto), \
9dec086e 113 TP_ARGS(data_args)); \
f99be407 114 } \
9dec086e 115 static inline int \
cc7b66ba 116 __register_trace_##name(void (*probe)(data_proto), void *data) \
f99be407 117 { \
81614639 118 return __tracepoint_probe_register(#name, (void *)probe,\
9dec086e
NC
119 data); \
120 \
f99be407 121 } \
9dec086e 122 static inline int \
cc7b66ba 123 __unregister_trace_##name(void (*probe)(data_proto), void *data)\
f99be407 124 { \
81614639 125 return __tracepoint_probe_unregister(#name, (void *)probe, \
9dec086e 126 data); \
f99be407
PMF
127 }
128
332b3a18 129/*
81614639 130 * The need for the _DECLARE_TRACEPOINT_NOARGS() is to handle the prototype
377d33ed 131 * (void). "void" is a special value in a function prototype and can
8d71973c 132 * not be combined with other arguments. Since the DECLARE_TRACEPOINT()
377d33ed
MD
133 * macro adds a data element at the beginning of the prototype,
134 * we need a way to differentiate "(void *data, proto)" from
135 * "(void *data, void)". The second prototype is invalid.
136 *
8d71973c 137 * DECLARE_TRACEPOINT_NOARGS() passes "void" as the tracepoint prototype
b979b346 138 * and "void *__tp_cb_data" as the callback prototype.
377d33ed 139 *
8d71973c 140 * DECLARE_TRACEPOINT() passes "proto" as the tracepoint protoype and
b979b346 141 * "void *__tp_cb_data, proto" as the callback prototype.
377d33ed 142 */
81614639
MD
143#define _DECLARE_TRACEPOINT_NOARGS(name) \
144 __DECLARE_TRACEPOINT(name, void, , void *__tp_cb_data, __tp_cb_data)
377d33ed 145
81614639
MD
146#define _DECLARE_TRACEPOINT(name, proto, args) \
147 __DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args), \
148 TP_PARAMS(void *__tp_cb_data, proto), \
149 TP_PARAMS(__tp_cb_data, args))
9dec086e 150
f99be407 151/*
81614639
MD
152 * __tracepoints_ptrs section is not const (read-only) to let the linker update
153 * the pointer, allowing PIC code.
f99be407 154 */
81614639
MD
155#define _DEFINE_TRACEPOINT(name) \
156 static const char __tpstrtab_##name[] \
157 __attribute__((section("__tracepoints_strings"))) = #name; \
158 struct tracepoint __tracepoint_##name \
159 __attribute__((section("__tracepoints"))) = \
160 { __tpstrtab_##name, 0, NULL }; \
161 static struct tracepoint * __tracepoint_ptr_##name \
162 __attribute__((used, section("__tracepoints_ptrs"))) = \
163 &__tracepoint_##name;
f99be407 164
f99be407 165
81614639
MD
166#define __register_tracepoint(name, probe, data) \
167 __register_trace_##name(probe, data)
168#define __unregister_tracepoint(name, probe, data) \
169 __unregister_trace_##name(probe, data)
f99be407 170
81614639
MD
171/*
172 * Connect a probe to a tracepoint.
173 * Internal API.
174 */
175extern
176int __tracepoint_probe_register(const char *name, void *probe, void *data);
f99be407
PMF
177
178/*
81614639
MD
179 * Disconnect a probe from a tracepoint.
180 * Internal API.
f99be407 181 */
81614639
MD
182extern
183int __tracepoint_probe_unregister(const char *name, void *probe, void *data);
f99be407 184
474d745f 185struct tracepoint_lib {
f218ff28 186 struct tracepoint * const *tracepoints_start;
474d745f 187 int tracepoints_count;
0222e121 188 struct cds_list_head list;
474d745f
PMF
189};
190
81614639
MD
191extern
192int tracepoint_register_lib(struct tracepoint * const *tracepoints_start,
193 int tracepoints_count);
194extern
195int tracepoint_unregister_lib(struct tracepoint * const *tracepoints_start);
872037bb 196
22d72948 197
8d71973c 198#ifndef TRACEPOINT_EVENT
22d72948 199/*
8d71973c 200 * For use with the TRACEPOINT_EVENT macro:
22d72948 201 *
0a42beb6
MD
202 * We define a tracepoint, its arguments, and its 'fast binary record'
203 * layout.
22d72948 204 *
0a42beb6 205 * Firstly, name your tracepoint via TRACEPOINT_EVENT(name,
22d72948 206 *
0a42beb6
MD
207 * The "name" MUST follow these rules to ensure no namespace clash
208 * occurs:
22d72948 209 *
0a42beb6
MD
210 * For projects (applications and libraries) for which an entity
211 * specific to the project controls the source code and thus its
212 * tracepoints (typically with a scope larger than a single company):
22d72948 213 *
0a42beb6
MD
214 * either:
215 * project_component_event
216 * or:
217 * project_event
218 *
219 * Where "project" is the name of the project,
220 * "component" is the name of the project component where the
221 * tracepoint is located (optional),
222 * "event" is the name of the tracepoint event.
223 *
224 * For projects issued from a single company wishing to advertise that
225 * the company controls the source code and thus the tracepoints, the
226 * "com_" prefix should be used:
227 *
228 * either:
229 * com_company_project_component_event
230 * or:
231 * com_company_project_event
232 *
233 * Where "company" is the name of the company,
234 * "project" is the name of the project,
235 * "component" is the name of the project component where the
236 * tracepoint is located (optional),
237 * "event" is the name of the tracepoint event.
238 *
239 *
240 * As an example, let's consider a user-space application "someproject"
241 * that would have an internal thread scheduler:
242 *
243 * TRACEPOINT_EVENT(someproject_sched_switch,
22d72948
NC
244 *
245 * *
246 * * A function has a regular function arguments
247 * * prototype, declare it via TP_PROTO():
248 * *
249 *
250 * TP_PROTO(struct rq *rq, struct task_struct *prev,
251 * struct task_struct *next),
252 *
253 * *
254 * * Define the call signature of the 'function'.
255 * * (Design sidenote: we use this instead of a
256 * * TP_PROTO1/TP_PROTO2/TP_PROTO3 ugliness.)
257 * *
258 *
259 * TP_ARGS(rq, prev, next),
260 *
261 * *
262 * * Fast binary tracing: define the trace record via
81614639 263 * * TP_FIELDS(). You can think about it like a
22d72948
NC
264 * * regular C structure local variable definition.
265 * *
266 * * This is how the trace record is structured and will
267 * * be saved into the ring buffer. These are the fields
268 * * that will be exposed to readers.
269 * *
0a42beb6 270 * * ctf_integer(pid_t, prev_pid, prev->pid) is equivalent
81614639 271 * * to a standard declaraton:
22d72948 272 * *
81614639 273 * * pid_t prev_pid;
22d72948 274 * *
81614639 275 * * followed by an assignment:
22d72948 276 * *
81614639 277 * * prev_pid = prev->pid;
22d72948 278 * *
0a42beb6 279 * * ctf_array(char, prev_comm, prev->comm, TASK_COMM_LEN) is
81614639 280 * * equivalent to:
22d72948 281 * *
81614639 282 * * char prev_comm[TASK_COMM_LEN];
22d72948 283 * *
81614639 284 * * followed by an assignment:
22d72948 285 * *
81614639 286 * * memcpy(prev_comm, prev->comm, TASK_COMM_LEN);
22d72948
NC
287 * *
288 *
81614639 289 * TP_FIELDS(
0a42beb6
MD
290 * ctf_array(char, prev_comm, prev->comm, TASK_COMM_LEN)
291 * ctf_integer(pid_t, prev_pid, prev->pid)
292 * ctf_integer(int, prev_prio, prev->prio)
293 * ctf_array(char, next_comm, next->comm, TASK_COMM_LEN)
294 * ctf_integer(pid_t, next_pid, next->pid)
295 * ctf_integer(int, next_prio, next->prio)
22d72948 296 * )
0a42beb6 297 * )
22d72948
NC
298 */
299
81614639
MD
300#define TRACEPOINT_EVENT(name, proto, args, fields) \
301 _DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args))
0c0686ee 302
81614639
MD
303#define TRACEPOINT_EVENT_CLASS(name, proto, args, fields)
304#define TRACEPOINT_EVENT_INSTANCE(template, name, proto, args) \
305 _DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args))
0c0686ee 306
81614639
MD
307/*
308 * Declaration of tracepoints that take 0 argument.
309 */
310#define TRACEPOINT_EVENT_NOARGS(name, fields) \
311 _DECLARE_TRACEPOINT_NOARGS(name)
0c0686ee 312
81614639
MD
313#define TRACEPOINT_EVENT_CLASS_NOARGS(name, fields)
314#define TRACEPOINT_EVENT_INSTANCE_NOARGS(template, name) \
315 _DECLARE_TRACEPOINT_NOARGS(name)
0c0686ee 316
81614639 317#endif /* #ifndef TRACEPOINT_EVENT */
22d72948 318
27b052e3 319#endif /* _UST_TRACEPOINT_H */
This page took 0.056392 seconds and 4 git commands to generate.