76645dd6a1d1fce7181022f88151aa033f508a46
[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 * These weak symbols, the constructor, and destructor take care of
50 * registering only _one_ instance of the tracepoints per shared-ojbect
51 * (or for the whole main program).
52 * The dummy tracepoint entry ensures that the start/stop pointers get
53 * initialized by the linker when no tracepoints are present in a
54 * shared-object (or main program).
55 */
56 extern struct tracepoint * const __start___tracepoints_ptrs[]
57 __attribute__((weak, visibility("hidden")));
58 extern struct tracepoint * const __stop___tracepoints_ptrs[]
59 __attribute__((weak, visibility("hidden")));
60 int __tracepoint_registered
61 __attribute__((weak, visibility("hidden")));
62
63 static void __attribute__((constructor)) __tracepoints__init(void)
64 {
65 if (__tracepoint_registered++)
66 return;
67 tracepoint_register_lib(__start___tracepoints_ptrs,
68 __stop___tracepoints_ptrs -
69 __start___tracepoints_ptrs);
70 }
71
72 static void __attribute__((destructor)) __tracepoints__destroy(void)
73 {
74 if (--__tracepoint_registered)
75 return;
76 tracepoint_unregister_lib(__start___tracepoints_ptrs);
77 }
78
79 /*
80 * it_func[0] is never NULL because there is at least one element in the array
81 * when the array itself is non NULL.
82 */
83 #define __DO_TRACE(tp, proto, args) \
84 do { \
85 struct tracepoint_probe *__tp_it_probe_ptr; \
86 void *__tp_it_func; \
87 void *__tp_cb_data; \
88 \
89 rcu_read_lock(); \
90 __tp_it_probe_ptr = rcu_dereference((tp)->probes); \
91 if (__tp_it_probe_ptr) { \
92 do { \
93 __tp_it_func = __tp_it_probe_ptr->func; \
94 __tp_cb_data = __tp_it_probe_ptr->data; \
95 ((void(*)(proto))__tp_it_func)(args); \
96 } while ((++__tp_it_probe_ptr)->func); \
97 } \
98 rcu_read_unlock(); \
99 } while (0)
100
101 #define TP_PARAMS(args...) args
102 #define TP_PROTO(args...) args
103 #define TP_ARGS(args...) args
104
105 #define __CHECK_TRACE(name, proto, args) \
106 do { \
107 if (unlikely(__tracepoint_##name.state)) \
108 __DO_TRACE(&__tracepoint_##name, \
109 TP_PROTO(proto), TP_ARGS(args)); \
110 } while (0)
111
112 /*
113 * Make sure the alignment of the structure in the __tracepoints section will
114 * not add unwanted padding between the beginning of the section and the
115 * structure. Force alignment to the same alignment as the section start.
116 */
117 #define __DECLARE_TRACEPOINT(name, proto, args, data_proto, data_args) \
118 extern struct tracepoint __tracepoint_##name; \
119 static inline void __trace_##name(proto) \
120 { \
121 __CHECK_TRACE(name, TP_PROTO(data_proto), \
122 TP_ARGS(data_args)); \
123 } \
124 static inline int \
125 __register_trace_##name(void (*probe)(data_proto), void *data) \
126 { \
127 return __tracepoint_probe_register(#name, (void *)probe,\
128 data); \
129 \
130 } \
131 static inline int \
132 __unregister_trace_##name(void (*probe)(data_proto), void *data)\
133 { \
134 return __tracepoint_probe_unregister(#name, (void *)probe, \
135 data); \
136 }
137
138 /*
139 * The need for the _DECLARE_TRACEPOINT_NOARGS() is to handle the prototype
140 * (void). "void" is a special value in a function prototype and can
141 * not be combined with other arguments. Since the DECLARE_TRACEPOINT()
142 * macro adds a data element at the beginning of the prototype,
143 * we need a way to differentiate "(void *data, proto)" from
144 * "(void *data, void)". The second prototype is invalid.
145 *
146 * DECLARE_TRACEPOINT_NOARGS() passes "void" as the tracepoint prototype
147 * and "void *__tp_cb_data" as the callback prototype.
148 *
149 * DECLARE_TRACEPOINT() passes "proto" as the tracepoint protoype and
150 * "void *__tp_cb_data, proto" as the callback prototype.
151 */
152 #define _DECLARE_TRACEPOINT_NOARGS(name) \
153 __DECLARE_TRACEPOINT(name, void, , void *__tp_cb_data, __tp_cb_data)
154
155 #define _DECLARE_TRACEPOINT(name, proto, args) \
156 __DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args), \
157 TP_PARAMS(void *__tp_cb_data, proto), \
158 TP_PARAMS(__tp_cb_data, args))
159
160 /*
161 * __tracepoints_ptrs section is not const (read-only) to let the linker update
162 * the pointer, allowing PIC code.
163 */
164 #define _DEFINE_TRACEPOINT(name) \
165 static const char __tpstrtab_##name[] \
166 __attribute__((section("__tracepoints_strings"))) = #name; \
167 struct tracepoint __tracepoint_##name \
168 __attribute__((section("__tracepoints"))) = \
169 { __tpstrtab_##name, 0, NULL }; \
170 static struct tracepoint * __tracepoint_ptr_##name \
171 __attribute__((used, section("__tracepoints_ptrs"))) = \
172 &__tracepoint_##name;
173
174
175 #define __register_tracepoint(name, probe, data) \
176 __register_trace_##name(probe, data)
177 #define __unregister_tracepoint(name, probe, data) \
178 __unregister_trace_##name(probe, data)
179
180 /*
181 * Connect a probe to a tracepoint.
182 * Internal API.
183 */
184 extern
185 int __tracepoint_probe_register(const char *name, void *probe, void *data);
186
187 /*
188 * Disconnect a probe from a tracepoint.
189 * Internal API.
190 */
191 extern
192 int __tracepoint_probe_unregister(const char *name, void *probe, void *data);
193
194 extern
195 int tracepoint_register_lib(struct tracepoint * const *tracepoints_start,
196 int tracepoints_count);
197 extern
198 int tracepoint_unregister_lib(struct tracepoint * const *tracepoints_start);
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" MUST follow these rules to ensure no namespace clash
240 * occurs:
241 *
242 * For projects (applications and libraries) for which an entity
243 * specific to the project controls the source code and thus its
244 * tracepoints (typically with a scope larger than a single company):
245 *
246 * either:
247 * project_component_event
248 * or:
249 * project_event
250 *
251 * Where "project" is the name of the project,
252 * "component" is the name of the project component (which may
253 * include several levels of sub-components, e.g.
254 * ...component_subcomponent_...) where the tracepoint is located
255 * (optional),
256 * "event" is the name of the tracepoint event.
257 *
258 * For projects issued from a single company wishing to advertise that
259 * the company controls the source code and thus the tracepoints, the
260 * "com_" prefix should be used:
261 *
262 * either:
263 * com_company_project_component_event
264 * or:
265 * com_company_project_event
266 *
267 * Where "company" is the name of the company,
268 * "project" is the name of the project,
269 * "component" is the name of the project component (which may
270 * include several levels of sub-components, e.g.
271 * ...component_subcomponent_...) where the tracepoint is located
272 * (optional),
273 * "event" is the name of the tracepoint event.
274 *
275 *
276 * As an example, let's consider a user-space application "someproject"
277 * that would have an internal thread scheduler:
278 *
279 * TRACEPOINT_EVENT(someproject_sched_switch,
280 *
281 * *
282 * * A function has a regular function arguments
283 * * prototype, declare it via TP_PROTO():
284 * *
285 *
286 * TP_PROTO(struct rq *rq, struct task_struct *prev,
287 * struct task_struct *next),
288 *
289 * *
290 * * Define the call signature of the 'function'.
291 * * (Design sidenote: we use this instead of a
292 * * TP_PROTO1/TP_PROTO2/TP_PROTO3 ugliness.)
293 * *
294 *
295 * TP_ARGS(rq, prev, next),
296 *
297 * *
298 * * Fast binary tracing: define the trace record via
299 * * TP_FIELDS(). You can think about it like a
300 * * regular C structure local variable definition.
301 * *
302 * * This is how the trace record is structured and will
303 * * be saved into the ring buffer. These are the fields
304 * * that will be exposed to readers.
305 * *
306 * * ctf_integer(pid_t, prev_pid, prev->pid) is equivalent
307 * * to a standard declaraton:
308 * *
309 * * pid_t prev_pid;
310 * *
311 * * followed by an assignment:
312 * *
313 * * prev_pid = prev->pid;
314 * *
315 * * ctf_array(char, prev_comm, prev->comm, TASK_COMM_LEN) is
316 * * equivalent to:
317 * *
318 * * char prev_comm[TASK_COMM_LEN];
319 * *
320 * * followed by an assignment:
321 * *
322 * * memcpy(prev_comm, prev->comm, TASK_COMM_LEN);
323 * *
324 *
325 * TP_FIELDS(
326 * ctf_array(char, prev_comm, prev->comm, TASK_COMM_LEN)
327 * ctf_integer(pid_t, prev_pid, prev->pid)
328 * ctf_integer(int, prev_prio, prev->prio)
329 * ctf_array(char, next_comm, next->comm, TASK_COMM_LEN)
330 * ctf_integer(pid_t, next_pid, next->pid)
331 * ctf_integer(int, next_prio, next->prio)
332 * )
333 * )
334 */
335
336 #define TRACEPOINT_EVENT(name, proto, args, fields) \
337 _DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args))
338
339 #define TRACEPOINT_EVENT_CLASS(name, proto, args, fields)
340 #define TRACEPOINT_EVENT_INSTANCE(template, name, proto, args) \
341 _DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args))
342
343 /*
344 * Declaration of tracepoints that take 0 argument.
345 */
346 #define TRACEPOINT_EVENT_NOARGS(name, fields) \
347 _DECLARE_TRACEPOINT_NOARGS(name)
348
349 #define TRACEPOINT_EVENT_CLASS_NOARGS(name, fields)
350 #define TRACEPOINT_EVENT_INSTANCE_NOARGS(template, name) \
351 _DECLARE_TRACEPOINT_NOARGS(name)
352
353 #endif /* #ifndef TRACEPOINT_EVENT */
354
355 #endif /* _UST_TRACEPOINT_H */
This page took 0.035022 seconds and 3 git commands to generate.