Tracepoint: make tracepoint ptrs section rw
[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>
518d7abb 29#include <ust/core.h>
b0c4126f 30#include <urcu/list.h>
f99be407 31
f99be407
PMF
32struct tracepoint;
33
b979b346 34struct tracepoint_probe {
9dec086e
NC
35 void *func;
36 void *data;
37};
38
f99be407
PMF
39struct tracepoint {
40 const char *name; /* Tracepoint name */
f36c12ab 41 char state; /* State. */
b979b346 42 struct tracepoint_probe *probes;
f218ff28 43};
f99be407 44
b979b346 45#define TP_PARAMS(args...) args
7166e240
PMF
46#define TP_PROTO(args...) args
47#define TP_ARGS(args...) args
f99be407 48
cc7b66ba
MD
49/*
50 * Tracepoints should be added to the instrumented code using the
51 * "tracepoint()" macro.
52 */
53#define tracepoint(name, args...) __trace_##name(args)
54
55#define register_tracepoint(name, probe, data) \
56 __register_trace_##name(probe, data)
57
58#define unregister_tracepoint(name, probe, data) \
59 __unregister_trace_##name(probe, data)
60
872037bb
PMF
61#define CONFIG_TRACEPOINTS
62#ifdef CONFIG_TRACEPOINTS
f99be407
PMF
63
64/*
65 * it_func[0] is never NULL because there is at least one element in the array
66 * when the array itself is non NULL.
67 */
68#define __DO_TRACE(tp, proto, args) \
69 do { \
b979b346
MD
70 struct tracepoint_probe *__tp_it_probe_ptr; \
71 void *__tp_it_func; \
72 void *__tp_cb_data; \
f99be407 73 \
9dec086e 74 rcu_read_lock(); \
b979b346
MD
75 __tp_it_probe_ptr = rcu_dereference((tp)->probes); \
76 if (__tp_it_probe_ptr) { \
f99be407 77 do { \
b979b346
MD
78 __tp_it_func = __tp_it_probe_ptr->func; \
79 __tp_cb_data = __tp_it_probe_ptr->data; \
80 ((void(*)(proto))__tp_it_func)(args); \
81 } while ((++__tp_it_probe_ptr)->func); \
f99be407 82 } \
9dec086e 83 rcu_read_unlock(); \
f99be407
PMF
84 } while (0)
85
f36c12ab 86#define __CHECK_TRACE(name, proto, args) \
f99be407 87 do { \
f36c12ab
MD
88 if (unlikely(__tracepoint_##name.state)) \
89 __DO_TRACE(&__tracepoint_##name, \
90 TP_PROTO(proto), TP_ARGS(args)); \
f99be407
PMF
91 } while (0)
92
93/*
94 * Make sure the alignment of the structure in the __tracepoints section will
95 * not add unwanted padding between the beginning of the section and the
96 * structure. Force alignment to the same alignment as the section start.
f99be407 97 */
8d71973c 98#define __DECLARE_TRACEPOINT(name, proto, args, data_proto, data_args) \
f99be407 99 extern struct tracepoint __tracepoint_##name; \
cc7b66ba 100 static inline void __trace_##name(proto) \
f99be407 101 { \
f36c12ab 102 __CHECK_TRACE(name, TP_PROTO(data_proto), \
9dec086e 103 TP_ARGS(data_args)); \
f99be407 104 } \
9dec086e 105 static inline int \
cc7b66ba 106 __register_trace_##name(void (*probe)(data_proto), void *data) \
f99be407 107 { \
9dec086e
NC
108 return tracepoint_probe_register(#name, (void *)probe, \
109 data); \
110 \
f99be407 111 } \
9dec086e 112 static inline int \
cc7b66ba 113 __unregister_trace_##name(void (*probe)(data_proto), void *data)\
f99be407 114 { \
9dec086e
NC
115 return tracepoint_probe_unregister(#name, (void *)probe, \
116 data); \
f99be407
PMF
117 }
118
332b3a18
MD
119/*
120 * __tracepoints_ptrs section is not const (read-only) to let the linker update
121 * the pointer, allowing PIC code.
122 */
8d71973c 123#define DEFINE_TRACEPOINT_FN(name, reg, unreg) \
f99be407
PMF
124 static const char __tpstrtab_##name[] \
125 __attribute__((section("__tracepoints_strings"))) = #name; \
126 struct tracepoint __tracepoint_##name \
f218ff28
MD
127 __attribute__((section("__tracepoints"))) = \
128 { __tpstrtab_##name, 0, NULL }; \
332b3a18 129 static struct tracepoint * __tracepoint_ptr_##name \
f218ff28
MD
130 __attribute__((used, section("__tracepoints_ptrs"))) = \
131 &__tracepoint_##name;
f99be407 132
8d71973c
MD
133#define DEFINE_TRACEPOINT(name) \
134 DEFINE_TRACEPOINT_FN(name, NULL, NULL)
9dec086e 135
f218ff28
MD
136extern void tracepoint_update_probe_range(struct tracepoint * const *begin,
137 struct tracepoint * const *end);
f99be407 138
872037bb 139#else /* !CONFIG_TRACEPOINTS */
8d71973c 140#define __DECLARE_TRACEPOINT(name, proto, args) \
872037bb
PMF
141 static inline void trace_##name(proto) \
142 { } \
143 static inline void _trace_##name(proto) \
144 { } \
cc7b66ba 145 static inline int __register_trace_##name(void (*probe)(proto), void *data) \
872037bb
PMF
146 { \
147 return -ENOSYS; \
148 } \
cc7b66ba 149 static inline int __unregister_trace_##name(void (*probe)(proto), void *data) \
872037bb
PMF
150 { \
151 return -ENOSYS; \
152 }
153
8d71973c 154#define DEFINE_TRACEPOINT(name)
872037bb
PMF
155#define EXPORT_TRACEPOINT_SYMBOL_GPL(name)
156#define EXPORT_TRACEPOINT_SYMBOL(name)
157
158static inline void tracepoint_update_probe_range(struct tracepoint *begin,
159 struct tracepoint *end)
160{ }
161#endif /* CONFIG_TRACEPOINTS */
f99be407 162
377d33ed 163/*
8d71973c 164 * The need for the DECLARE_TRACEPOINT_NOARGS() is to handle the prototype
377d33ed 165 * (void). "void" is a special value in a function prototype and can
8d71973c 166 * not be combined with other arguments. Since the DECLARE_TRACEPOINT()
377d33ed
MD
167 * macro adds a data element at the beginning of the prototype,
168 * we need a way to differentiate "(void *data, proto)" from
169 * "(void *data, void)". The second prototype is invalid.
170 *
8d71973c 171 * DECLARE_TRACEPOINT_NOARGS() passes "void" as the tracepoint prototype
b979b346 172 * and "void *__tp_cb_data" as the callback prototype.
377d33ed 173 *
8d71973c 174 * DECLARE_TRACEPOINT() passes "proto" as the tracepoint protoype and
b979b346 175 * "void *__tp_cb_data, proto" as the callback prototype.
377d33ed 176 */
8d71973c
MD
177#define DECLARE_TRACEPOINT_NOARGS(name) \
178 __DECLARE_TRACEPOINT(name, void, , void *__tp_cb_data, __tp_cb_data)
377d33ed 179
8d71973c
MD
180#define DECLARE_TRACEPOINT(name, proto, args) \
181 __DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args),\
b979b346
MD
182 TP_PARAMS(void *__tp_cb_data, proto), \
183 TP_PARAMS(__tp_cb_data, args))
9dec086e 184
f99be407
PMF
185/*
186 * Connect a probe to a tracepoint.
187 * Internal API, should not be used directly.
188 */
9dec086e 189extern int tracepoint_probe_register(const char *name, void *probe, void *data);
f99be407
PMF
190
191/*
192 * Disconnect a probe from a tracepoint.
193 * Internal API, should not be used directly.
194 */
9dec086e 195extern int tracepoint_probe_unregister(const char *name, void *probe, void *data);
f99be407 196
9dec086e
NC
197extern int tracepoint_probe_register_noupdate(const char *name, void *probe,
198 void *data);
199extern int tracepoint_probe_unregister_noupdate(const char *name, void *probe,
200 void *data);
f99be407
PMF
201extern void tracepoint_probe_update_all(void);
202
203struct tracepoint_iter {
474d745f 204 struct tracepoint_lib *lib;
f218ff28 205 struct tracepoint * const *tracepoint;
f99be407
PMF
206};
207
208extern void tracepoint_iter_start(struct tracepoint_iter *iter);
209extern void tracepoint_iter_next(struct tracepoint_iter *iter);
210extern void tracepoint_iter_stop(struct tracepoint_iter *iter);
211extern void tracepoint_iter_reset(struct tracepoint_iter *iter);
f218ff28
MD
212extern int tracepoint_get_iter_range(struct tracepoint * const **tracepoint,
213 struct tracepoint * const *begin, struct tracepoint * const *end);
f99be407
PMF
214
215/*
216 * tracepoint_synchronize_unregister must be called between the last tracepoint
217 * probe unregistration and the end of module exit to make sure there is no
218 * caller executing a probe when it is freed.
219 */
220static inline void tracepoint_synchronize_unregister(void)
221{
270c6b98 222//ust// synchronize_sched();
f99be407
PMF
223}
224
474d745f 225struct tracepoint_lib {
f218ff28 226 struct tracepoint * const *tracepoints_start;
474d745f 227 int tracepoints_count;
0222e121 228 struct cds_list_head list;
474d745f
PMF
229};
230
f218ff28 231extern int tracepoint_register_lib(struct tracepoint * const *tracepoints_start,
872037bb 232 int tracepoints_count);
f218ff28 233extern int tracepoint_unregister_lib(struct tracepoint * const *tracepoints_start);
872037bb
PMF
234
235#define TRACEPOINT_LIB \
bf5fe926
MD
236 extern struct tracepoint * const __start___tracepoints_ptrs[] __attribute__((weak, visibility("hidden"))); \
237 extern struct tracepoint * const __stop___tracepoints_ptrs[] __attribute__((weak, visibility("hidden"))); \
332b3a18
MD
238 static struct tracepoint * __tracepoint_ptr_dummy \
239 __attribute__((used, section("__tracepoints_ptrs"))); \
bf5fe926
MD
240 static void __attribute__((constructor)) __tracepoints__init(void) \
241 { \
103fffbc
MD
242 tracepoint_register_lib(__start___tracepoints_ptrs, \
243 __stop___tracepoints_ptrs - \
244 __start___tracepoints_ptrs); \
bf5fe926
MD
245 } \
246 \
247 static void __attribute__((destructor)) __tracepoints__destroy(void) \
248 { \
103fffbc 249 tracepoint_unregister_lib(__start___tracepoints_ptrs); \
872037bb
PMF
250 }
251
22d72948 252
8d71973c 253#ifndef TRACEPOINT_EVENT
22d72948 254/*
8d71973c 255 * For use with the TRACEPOINT_EVENT macro:
22d72948
NC
256 *
257 * We define a tracepoint, its arguments, its printf format
258 * and its 'fast binary record' layout.
259 *
8d71973c 260 * Firstly, name your tracepoint via TRACEPOINT_EVENT(name : the
22d72948
NC
261 * 'subsystem_event' notation is fine.
262 *
263 * Think about this whole construct as the
264 * 'trace_sched_switch() function' from now on.
265 *
266 *
8d71973c 267 * TRACEPOINT_EVENT(sched_switch,
22d72948
NC
268 *
269 * *
270 * * A function has a regular function arguments
271 * * prototype, declare it via TP_PROTO():
272 * *
273 *
274 * TP_PROTO(struct rq *rq, struct task_struct *prev,
275 * struct task_struct *next),
276 *
277 * *
278 * * Define the call signature of the 'function'.
279 * * (Design sidenote: we use this instead of a
280 * * TP_PROTO1/TP_PROTO2/TP_PROTO3 ugliness.)
281 * *
282 *
283 * TP_ARGS(rq, prev, next),
284 *
285 * *
286 * * Fast binary tracing: define the trace record via
287 * * TP_STRUCT__entry(). You can think about it like a
288 * * regular C structure local variable definition.
289 * *
290 * * This is how the trace record is structured and will
291 * * be saved into the ring buffer. These are the fields
292 * * that will be exposed to readers.
293 * *
294 * * The declared 'local variable' is called '__entry'
295 * *
296 * * __field(pid_t, prev_prid) is equivalent to a standard declariton:
297 * *
298 * * pid_t prev_pid;
299 * *
300 * * __array(char, prev_comm, TASK_COMM_LEN) is equivalent to:
301 * *
302 * * char prev_comm[TASK_COMM_LEN];
303 * *
304 *
305 * TP_STRUCT__entry(
306 * __array( char, prev_comm, TASK_COMM_LEN )
307 * __field( pid_t, prev_pid )
308 * __field( int, prev_prio )
309 * __array( char, next_comm, TASK_COMM_LEN )
310 * __field( pid_t, next_pid )
311 * __field( int, next_prio )
312 * ),
313 *
314 * *
315 * * Assign the entry into the trace record, by embedding
316 * * a full C statement block into TP_fast_assign(). You
317 * * can refer to the trace record as '__entry' -
318 * * otherwise you can put arbitrary C code in here.
319 * *
320 * * Note: this C code will execute every time a trace event
321 * * happens, on an active tracepoint.
322 * *
323 *
324 * TP_fast_assign(
325 * memcpy(__entry->next_comm, next->comm, TASK_COMM_LEN);
326 * __entry->prev_pid = prev->pid;
327 * __entry->prev_prio = prev->prio;
328 * memcpy(__entry->prev_comm, prev->comm, TASK_COMM_LEN);
329 * __entry->next_pid = next->pid;
330 * __entry->next_prio = next->prio;
331 * )
332 *
333 * *
334 * * Formatted output of a trace record via TP_printf().
335 * * This is how the tracepoint will appear under debugging
336 * * of tracepoints.
337 * *
338 * * (raw-binary tracing wont actually perform this step.)
339 * *
340 *
341 * TP_printf("task %s:%d [%d] ==> %s:%d [%d]",
342 * __entry->prev_comm, __entry->prev_pid, __entry->prev_prio,
343 * __entry->next_comm, __entry->next_pid, __entry->next_prio),
344 *
345 * );
346 *
347 * This macro construct is thus used for the regular printf format
348 * tracing setup.
349 *
350 * A set of (un)registration functions can be passed to the variant
8d71973c 351 * TRACEPOINT_EVENT_FN to perform any (un)registration work.
22d72948
NC
352 */
353
0c0686ee
NC
354struct trace_event {
355 const char *name;
356 int (*regfunc)(void *data);
357 int (*unregfunc)(void *data);
fc1caebc 358};
0c0686ee
NC
359
360struct trace_event_lib {
fc1caebc 361 struct trace_event * const *trace_events_start;
0c0686ee 362 int trace_events_count;
0222e121 363 struct cds_list_head list;
0c0686ee
NC
364};
365
366struct trace_event_iter {
367 struct trace_event_lib *lib;
fc1caebc 368 struct trace_event * const *trace_event;
0c0686ee
NC
369};
370
371extern void lock_trace_events(void);
372extern void unlock_trace_events(void);
373
374extern void trace_event_iter_start(struct trace_event_iter *iter);
375extern void trace_event_iter_next(struct trace_event_iter *iter);
376extern void trace_event_iter_reset(struct trace_event_iter *iter);
377
fc1caebc
MD
378extern int trace_event_get_iter_range(struct trace_event * const **trace_event,
379 struct trace_event * const *begin,
380 struct trace_event * const *end);
0c0686ee
NC
381
382extern void trace_event_update_process(void);
383extern int is_trace_event_enabled(const char *channel, const char *name);
384
fc1caebc 385extern int trace_event_register_lib(struct trace_event * const *start_trace_events,
0c0686ee
NC
386 int trace_event_count);
387
fc1caebc 388extern int trace_event_unregister_lib(struct trace_event * const *start_trace_events);
0c0686ee 389
8d71973c 390#define TRACEPOINT_EVENT_LIB \
fc1caebc 391 extern struct trace_event * const __start___trace_events_ptrs[] \
0c0686ee 392 __attribute__((weak, visibility("hidden"))); \
fc1caebc 393 extern struct trace_event * const __stop___trace_events_ptrs[] \
0c0686ee 394 __attribute__((weak, visibility("hidden"))); \
332b3a18
MD
395 static struct trace_event * __event_ptrs_dummy \
396 __attribute__((used, section("__trace_events_ptrs"))); \
0c0686ee
NC
397 static void __attribute__((constructor)) \
398 __trace_events__init(void) \
399 { \
fc1caebc
MD
400 trace_event_register_lib(__start___trace_events_ptrs, \
401 __stop___trace_events_ptrs - \
402 __start___trace_events_ptrs); \
0c0686ee
NC
403 } \
404 \
405 static void __attribute__((destructor)) \
406 __trace_event__destroy(void) \
407 { \
fc1caebc 408 trace_event_unregister_lib(__start___trace_events_ptrs);\
0c0686ee
NC
409 }
410
8d71973c
MD
411#define DECLARE_TRACEPOINT_EVENT_CLASS(name, proto, args, tstruct, assign, print)
412#define DEFINE_TRACEPOINT_EVENT(template, name, proto, args) \
413 DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args))
414#define DEFINE_TRACEPOINT_EVENT_PRINT(template, name, proto, args, print)\
415 DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args))
22d72948 416
8d71973c
MD
417#define TRACEPOINT_EVENT(name, proto, args, struct, assign, print) \
418 DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args))
419#define TRACEPOINT_EVENT_FN(name, proto, args, struct, \
22d72948 420 assign, print, reg, unreg) \
8d71973c 421 DECLARE_TRACEPOINT(name, TP_PARAMS(proto), TP_PARAMS(args))
22d72948 422
8d71973c 423#endif /* ifdef TRACEPOINT_EVENT (see note above) */
22d72948
NC
424
425
27b052e3 426#endif /* _UST_TRACEPOINT_H */
This page took 0.056822 seconds and 4 git commands to generate.