158d9b8a4cc1d3d28fdf4546f058dc8f83868152
[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 struct tracepoint_probe {
32 void *func;
33 void *data;
34 };
35
36 struct tracepoint {
37 const char *name; /* Tracepoint name */
38 char state; /* State. */
39 struct tracepoint_probe *probes;
40 };
41
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
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 }
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 { \
76 struct tracepoint_probe *__tp_it_probe_ptr; \
77 void *__tp_it_func; \
78 void *__tp_cb_data; \
79 \
80 rcu_read_lock(); \
81 __tp_it_probe_ptr = rcu_dereference((tp)->probes); \
82 if (__tp_it_probe_ptr) { \
83 do { \
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); \
88 } \
89 rcu_read_unlock(); \
90 } while (0)
91
92 #define TP_PARAMS(args...) args
93 #define TP_PROTO(args...) args
94 #define TP_ARGS(args...) args
95
96 #define __CHECK_TRACE(name, proto, args) \
97 do { \
98 if (unlikely(__tracepoint_##name.state)) \
99 __DO_TRACE(&__tracepoint_##name, \
100 TP_PROTO(proto), TP_ARGS(args)); \
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.
107 */
108 #define __DECLARE_TRACEPOINT(name, proto, args, data_proto, data_args) \
109 extern struct tracepoint __tracepoint_##name; \
110 static inline void __trace_##name(proto) \
111 { \
112 __CHECK_TRACE(name, TP_PROTO(data_proto), \
113 TP_ARGS(data_args)); \
114 } \
115 static inline int \
116 __register_trace_##name(void (*probe)(data_proto), void *data) \
117 { \
118 return __tracepoint_probe_register(#name, (void *)probe,\
119 data); \
120 \
121 } \
122 static inline int \
123 __unregister_trace_##name(void (*probe)(data_proto), void *data)\
124 { \
125 return __tracepoint_probe_unregister(#name, (void *)probe, \
126 data); \
127 }
128
129 /*
130 * The need for the _DECLARE_TRACEPOINT_NOARGS() is to handle the prototype
131 * (void). "void" is a special value in a function prototype and can
132 * not be combined with other arguments. Since the DECLARE_TRACEPOINT()
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 *
137 * DECLARE_TRACEPOINT_NOARGS() passes "void" as the tracepoint prototype
138 * and "void *__tp_cb_data" as the callback prototype.
139 *
140 * DECLARE_TRACEPOINT() passes "proto" as the tracepoint protoype and
141 * "void *__tp_cb_data, proto" as the callback prototype.
142 */
143 #define _DECLARE_TRACEPOINT_NOARGS(name) \
144 __DECLARE_TRACEPOINT(name, void, , void *__tp_cb_data, __tp_cb_data)
145
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))
150
151 /*
152 * __tracepoints_ptrs section is not const (read-only) to let the linker update
153 * the pointer, allowing PIC code.
154 */
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;
164
165
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)
170
171 /*
172 * Connect a probe to a tracepoint.
173 * Internal API.
174 */
175 extern
176 int __tracepoint_probe_register(const char *name, void *probe, void *data);
177
178 /*
179 * Disconnect a probe from a tracepoint.
180 * Internal API.
181 */
182 extern
183 int __tracepoint_probe_unregister(const char *name, void *probe, void *data);
184
185 struct tracepoint_lib {
186 struct tracepoint * const *tracepoints_start;
187 int tracepoints_count;
188 struct cds_list_head list;
189 };
190
191 extern
192 int tracepoint_register_lib(struct tracepoint * const *tracepoints_start,
193 int tracepoints_count);
194 extern
195 int tracepoint_unregister_lib(struct tracepoint * const *tracepoints_start);
196
197
198 #ifndef TRACEPOINT_EVENT
199 /*
200 * Usage of the TRACEPOINT_EVENT macro:
201 *
202 * In short, an example:
203 *
204 * TRACEPOINT_EVENT(< [com_company_]project_[component_]event >,
205 * TP_PROTO(int arg0, void *arg1, char *string, size_t strlen,
206 * long *arg4, size_t arg4_len),
207 * TP_ARGS(arg0, arg1, string, strlen, arg4, arg4_len),
208 * TP_FIELDS(
209 *
210 * * Integer, printed in base 10 *
211 * ctf_integer(int, field_a, arg0)
212 *
213 * * Integer, printed with 0x base 16 *
214 * ctf_integer_hex(unsigned long, field_d, arg1)
215 *
216 * * Array Sequence, printed as UTF8-encoded array of bytes *
217 * ctf_array_text(char, field_b, string, FIXED_LEN)
218 * ctf_sequence_text(char, field_c, string, size_t, strlen)
219 *
220 * * String, printed as UTF8-encoded string *
221 * ctf_string(field_e, string)
222 *
223 * * Array sequence of signed integer values *
224 * ctf_array(long, field_f, arg4, FIXED_LEN4)
225 * ctf_sequence(long, field_g, arg4, size_t, arg4_len)
226 * )
227 * )
228 *
229 * In more detail:
230 *
231 * We define a tracepoint, its arguments, and its 'fast binary record'
232 * layout.
233 *
234 * Firstly, name your tracepoint via TRACEPOINT_EVENT(name,
235 *
236 * The "name" MUST follow these rules to ensure no namespace clash
237 * occurs:
238 *
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):
242 *
243 * either:
244 * project_component_event
245 * or:
246 * project_event
247 *
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
252 * (optional),
253 * "event" is the name of the tracepoint event.
254 *
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:
258 *
259 * either:
260 * com_company_project_component_event
261 * or:
262 * com_company_project_event
263 *
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
269 * (optional),
270 * "event" is the name of the tracepoint event.
271 *
272 *
273 * As an example, let's consider a user-space application "someproject"
274 * that would have an internal thread scheduler:
275 *
276 * TRACEPOINT_EVENT(someproject_sched_switch,
277 *
278 * *
279 * * A function has a regular function arguments
280 * * prototype, declare it via TP_PROTO():
281 * *
282 *
283 * TP_PROTO(struct rq *rq, struct task_struct *prev,
284 * struct task_struct *next),
285 *
286 * *
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.)
290 * *
291 *
292 * TP_ARGS(rq, prev, next),
293 *
294 * *
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.
298 * *
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.
302 * *
303 * * ctf_integer(pid_t, prev_pid, prev->pid) is equivalent
304 * * to a standard declaraton:
305 * *
306 * * pid_t prev_pid;
307 * *
308 * * followed by an assignment:
309 * *
310 * * prev_pid = prev->pid;
311 * *
312 * * ctf_array(char, prev_comm, prev->comm, TASK_COMM_LEN) is
313 * * equivalent to:
314 * *
315 * * char prev_comm[TASK_COMM_LEN];
316 * *
317 * * followed by an assignment:
318 * *
319 * * memcpy(prev_comm, prev->comm, TASK_COMM_LEN);
320 * *
321 *
322 * TP_FIELDS(
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)
329 * )
330 * )
331 */
332
333 #define TRACEPOINT_EVENT(name, proto, args, fields) \
334 _DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args))
335
336 #define TRACEPOINT_EVENT_CLASS(name, proto, args, fields)
337 #define TRACEPOINT_EVENT_INSTANCE(template, name, proto, args) \
338 _DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args))
339
340 /*
341 * Declaration of tracepoints that take 0 argument.
342 */
343 #define TRACEPOINT_EVENT_NOARGS(name, fields) \
344 _DECLARE_TRACEPOINT_NOARGS(name)
345
346 #define TRACEPOINT_EVENT_CLASS_NOARGS(name, fields)
347 #define TRACEPOINT_EVENT_INSTANCE_NOARGS(template, name) \
348 _DECLARE_TRACEPOINT_NOARGS(name)
349
350 #endif /* #ifndef TRACEPOINT_EVENT */
351
352 #endif /* _UST_TRACEPOINT_H */
This page took 0.035681 seconds and 3 git commands to generate.