4d84eaddbe19bdbf9c0fb253c2eb967978a85f1e
[ust.git] / include / ust / tracepoint.h
1 #ifndef _UST_TRACEPOINT_H
2 #define _UST_TRACEPOINT_H
3
4 /*
5 * Copyright (C) 2008 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
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 <ust/core.h>
30 #include <urcu/list.h>
31
32 struct tracepoint_probe {
33 void *func;
34 void *data;
35 };
36
37 struct tracepoint {
38 const char *name; /* Tracepoint name */
39 char state; /* State. */
40 struct tracepoint_probe *probes;
41 };
42
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
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 }
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.
74 */
75 #define __DO_TRACE(tp, proto, args) \
76 do { \
77 struct tracepoint_probe *__tp_it_probe_ptr; \
78 void *__tp_it_func; \
79 void *__tp_cb_data; \
80 \
81 rcu_read_lock(); \
82 __tp_it_probe_ptr = rcu_dereference((tp)->probes); \
83 if (__tp_it_probe_ptr) { \
84 do { \
85 __tp_it_func = __tp_it_probe_ptr->func; \
86 __tp_cb_data = __tp_it_probe_ptr->data; \
87 ((void(*)(proto))__tp_it_func)(args); \
88 } while ((++__tp_it_probe_ptr)->func); \
89 } \
90 rcu_read_unlock(); \
91 } while (0)
92
93 #define TP_PARAMS(args...) args
94 #define TP_PROTO(args...) args
95 #define TP_ARGS(args...) args
96
97 #define __CHECK_TRACE(name, proto, args) \
98 do { \
99 if (unlikely(__tracepoint_##name.state)) \
100 __DO_TRACE(&__tracepoint_##name, \
101 TP_PROTO(proto), TP_ARGS(args)); \
102 } while (0)
103
104 /*
105 * Make sure the alignment of the structure in the __tracepoints section will
106 * not add unwanted padding between the beginning of the section and the
107 * structure. Force alignment to the same alignment as the section start.
108 */
109 #define __DECLARE_TRACEPOINT(name, proto, args, data_proto, data_args) \
110 extern struct tracepoint __tracepoint_##name; \
111 static inline void __trace_##name(proto) \
112 { \
113 __CHECK_TRACE(name, TP_PROTO(data_proto), \
114 TP_ARGS(data_args)); \
115 } \
116 static inline int \
117 __register_trace_##name(void (*probe)(data_proto), void *data) \
118 { \
119 return __tracepoint_probe_register(#name, (void *)probe,\
120 data); \
121 \
122 } \
123 static inline int \
124 __unregister_trace_##name(void (*probe)(data_proto), void *data)\
125 { \
126 return __tracepoint_probe_unregister(#name, (void *)probe, \
127 data); \
128 }
129
130 /*
131 * The need for the _DECLARE_TRACEPOINT_NOARGS() is to handle the prototype
132 * (void). "void" is a special value in a function prototype and can
133 * not be combined with other arguments. Since the DECLARE_TRACEPOINT()
134 * macro adds a data element at the beginning of the prototype,
135 * we need a way to differentiate "(void *data, proto)" from
136 * "(void *data, void)". The second prototype is invalid.
137 *
138 * DECLARE_TRACEPOINT_NOARGS() passes "void" as the tracepoint prototype
139 * and "void *__tp_cb_data" as the callback prototype.
140 *
141 * DECLARE_TRACEPOINT() passes "proto" as the tracepoint protoype and
142 * "void *__tp_cb_data, proto" as the callback prototype.
143 */
144 #define _DECLARE_TRACEPOINT_NOARGS(name) \
145 __DECLARE_TRACEPOINT(name, void, , void *__tp_cb_data, __tp_cb_data)
146
147 #define _DECLARE_TRACEPOINT(name, proto, args) \
148 __DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args), \
149 TP_PARAMS(void *__tp_cb_data, proto), \
150 TP_PARAMS(__tp_cb_data, args))
151
152 /*
153 * __tracepoints_ptrs section is not const (read-only) to let the linker update
154 * the pointer, allowing PIC code.
155 */
156 #define _DEFINE_TRACEPOINT(name) \
157 static const char __tpstrtab_##name[] \
158 __attribute__((section("__tracepoints_strings"))) = #name; \
159 struct tracepoint __tracepoint_##name \
160 __attribute__((section("__tracepoints"))) = \
161 { __tpstrtab_##name, 0, NULL }; \
162 static struct tracepoint * __tracepoint_ptr_##name \
163 __attribute__((used, section("__tracepoints_ptrs"))) = \
164 &__tracepoint_##name;
165
166
167 #define __register_tracepoint(name, probe, data) \
168 __register_trace_##name(probe, data)
169 #define __unregister_tracepoint(name, probe, data) \
170 __unregister_trace_##name(probe, data)
171
172 /*
173 * Connect a probe to a tracepoint.
174 * Internal API.
175 */
176 extern
177 int __tracepoint_probe_register(const char *name, void *probe, void *data);
178
179 /*
180 * Disconnect a probe from a tracepoint.
181 * Internal API.
182 */
183 extern
184 int __tracepoint_probe_unregister(const char *name, void *probe, void *data);
185
186 struct tracepoint_lib {
187 struct tracepoint * const *tracepoints_start;
188 int tracepoints_count;
189 struct cds_list_head list;
190 };
191
192 extern
193 int tracepoint_register_lib(struct tracepoint * const *tracepoints_start,
194 int tracepoints_count);
195 extern
196 int tracepoint_unregister_lib(struct tracepoint * const *tracepoints_start);
197
198
199 #ifndef TRACEPOINT_EVENT
200 /*
201 * For use with the TRACEPOINT_EVENT macro:
202 *
203 * We define a tracepoint, its arguments, its printf format
204 * and its 'fast binary record' layout.
205 *
206 * Firstly, name your tracepoint via TRACEPOINT_EVENT(name : the
207 * 'subsystem_event' notation is fine.
208 *
209 * Think about this whole construct as the
210 * 'trace_sched_switch() function' from now on.
211 *
212 *
213 * TRACEPOINT_EVENT(sched_switch,
214 *
215 * *
216 * * A function has a regular function arguments
217 * * prototype, declare it via TP_PROTO():
218 * *
219 *
220 * TP_PROTO(struct rq *rq, struct task_struct *prev,
221 * struct task_struct *next),
222 *
223 * *
224 * * Define the call signature of the 'function'.
225 * * (Design sidenote: we use this instead of a
226 * * TP_PROTO1/TP_PROTO2/TP_PROTO3 ugliness.)
227 * *
228 *
229 * TP_ARGS(rq, prev, next),
230 *
231 * *
232 * * Fast binary tracing: define the trace record via
233 * * TP_FIELDS(). You can think about it like a
234 * * regular C structure local variable definition.
235 * *
236 * * This is how the trace record is structured and will
237 * * be saved into the ring buffer. These are the fields
238 * * that will be exposed to readers.
239 * *
240 * * tp_field(pid_t, prev_pid, prev->pid) is equivalent
241 * * to a standard declaraton:
242 * *
243 * * pid_t prev_pid;
244 * *
245 * * followed by an assignment:
246 * *
247 * * prev_pid = prev->pid;
248 * *
249 * * tp_array(char, prev_comm, TASK_COMM_LEN, prev->comm) is
250 * * equivalent to:
251 * *
252 * * char prev_comm[TASK_COMM_LEN];
253 * *
254 * * followed by an assignment:
255 * *
256 * * memcpy(prev_comm, prev->comm, TASK_COMM_LEN);
257 * *
258 *
259 * TP_FIELDS(
260 * tp_array(char, prev_comm, TASK_COMM_LEN, prev->comm)
261 * tp_field(pid_t, prev_pid, prev->pid)
262 * tp_field(int, prev_prio, prev->prio)
263 * tp_array(char, next_comm, TASK_COMM_LEN, next->comm)
264 * tp_field(pid_t, next_pid, next->pid)
265 * tp_field(int, next_prio, next->prio)
266 * )
267 * );
268 */
269
270 #define TRACEPOINT_EVENT(name, proto, args, fields) \
271 _DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args))
272
273 #define TRACEPOINT_EVENT_CLASS(name, proto, args, fields)
274 #define TRACEPOINT_EVENT_INSTANCE(template, name, proto, args) \
275 _DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args))
276
277 /*
278 * Declaration of tracepoints that take 0 argument.
279 */
280 #define TRACEPOINT_EVENT_NOARGS(name, fields) \
281 _DECLARE_TRACEPOINT_NOARGS(name)
282
283 #define TRACEPOINT_EVENT_CLASS_NOARGS(name, fields)
284 #define TRACEPOINT_EVENT_INSTANCE_NOARGS(template, name) \
285 _DECLARE_TRACEPOINT_NOARGS(name)
286
287
288
289 #define TRACEPOINT_EVENT_LIB \
290 extern struct trace_event * const __start___trace_events_ptrs[] \
291 __attribute__((weak, visibility("hidden"))); \
292 extern struct trace_event * const __stop___trace_events_ptrs[] \
293 __attribute__((weak, visibility("hidden"))); \
294 static struct trace_event * __event_ptrs_dummy \
295 __attribute__((used, section("__trace_events_ptrs"))); \
296 static void __attribute__((constructor)) \
297 __trace_events__init(void) \
298 { \
299 trace_event_register_lib(__start___trace_events_ptrs, \
300 __stop___trace_events_ptrs - \
301 __start___trace_events_ptrs); \
302 } \
303 \
304 static void __attribute__((destructor)) \
305 __trace_event__destroy(void) \
306 { \
307 trace_event_unregister_lib(__start___trace_events_ptrs);\
308 }
309
310 struct trace_event {
311 const char *name;
312 };
313
314 struct trace_event_lib {
315 struct trace_event * const *trace_events_start;
316 int trace_events_count;
317 struct cds_list_head list;
318 };
319
320 extern
321 int trace_event_register_lib(struct trace_event * const *start_trace_events,
322 int trace_event_count);
323 extern
324 int trace_event_unregister_lib(struct trace_event * const *start_trace_events);
325
326 #endif /* #ifndef TRACEPOINT_EVENT */
327
328 #endif /* _UST_TRACEPOINT_H */
This page took 0.034234 seconds and 3 git commands to generate.