Add a simple test case for TRACE_EVENT v2
[lttng-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; either
12 * version 2.1 of the License, or (at your option) any later version.
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 #define _LGPL_SOURCE
29 #include <urcu-bp.h>
30 #include <ust/immediate.h>
31 #include <ust/core.h>
32
33 struct module;
34 struct tracepoint;
35
36 struct tracepoint {
37 const char *name; /* Tracepoint name */
38 DEFINE_IMV(char, state); /* State. */
39 void **funcs;
40 } __attribute__((aligned(32))); /*
41 * Aligned on 32 bytes because it is
42 * globally visible and gcc happily
43 * align these on the structure size.
44 * Keep in sync with vmlinux.lds.h.
45 */
46
47 #define PARAMS(args...) args
48
49 #define TP_PROTO(args...) args
50 #define TP_ARGS(args...) args
51
52 #define CONFIG_TRACEPOINTS
53 #ifdef CONFIG_TRACEPOINTS
54
55 /*
56 * it_func[0] is never NULL because there is at least one element in the array
57 * when the array itself is non NULL.
58 */
59 #define __DO_TRACE(tp, proto, args) \
60 do { \
61 void **it_func; \
62 \
63 rcu_read_lock(); /*ust rcu_read_lock_sched_notrace(); */ \
64 it_func = rcu_dereference((tp)->funcs); \
65 if (it_func) { \
66 do { \
67 ((void(*)(proto))(*it_func))(args); \
68 } while (*(++it_func)); \
69 } \
70 rcu_read_unlock(); /*ust rcu_read_unlock_sched_notrace(); */ \
71 } while (0)
72
73 #define __CHECK_TRACE(name, generic, proto, args) \
74 do { \
75 if (!generic) { \
76 if (unlikely(imv_read(__tracepoint_##name.state))) \
77 __DO_TRACE(&__tracepoint_##name, \
78 TP_PROTO(proto), TP_ARGS(args)); \
79 } else { \
80 if (unlikely(_imv_read(__tracepoint_##name.state))) \
81 __DO_TRACE(&__tracepoint_##name, \
82 TP_PROTO(proto), TP_ARGS(args)); \
83 } \
84 } while (0)
85
86 /*
87 * Make sure the alignment of the structure in the __tracepoints section will
88 * not add unwanted padding between the beginning of the section and the
89 * structure. Force alignment to the same alignment as the section start.
90 *
91 * The "generic" argument, passed to the declared __trace_##name inline
92 * function controls which tracepoint enabling mechanism must be used.
93 * If generic is true, a variable read is used.
94 * If generic is false, immediate values are used.
95 */
96 #define DECLARE_TRACE(name, proto, args) \
97 extern struct tracepoint __tracepoint_##name; \
98 static inline void trace_##name(proto) \
99 { \
100 __CHECK_TRACE(name, 0, TP_PROTO(proto), TP_ARGS(args)); \
101 } \
102 static inline void _trace_##name(proto) \
103 { \
104 __CHECK_TRACE(name, 1, TP_PROTO(proto), TP_ARGS(args)); \
105 } \
106 static inline int register_trace_##name(void (*probe)(proto)) \
107 { \
108 return tracepoint_probe_register(#name, (void *)probe); \
109 } \
110 static inline int unregister_trace_##name(void (*probe)(proto)) \
111 { \
112 return tracepoint_probe_unregister(#name, (void *)probe);\
113 }
114
115 #define DEFINE_TRACE(name) \
116 static const char __tpstrtab_##name[] \
117 __attribute__((section("__tracepoints_strings"))) = #name; \
118 struct tracepoint __tracepoint_##name \
119 __attribute__((section("__tracepoints"), aligned(32))) = \
120 { __tpstrtab_##name, 0, NULL }
121
122 extern void tracepoint_update_probe_range(struct tracepoint *begin,
123 struct tracepoint *end);
124
125 #else /* !CONFIG_TRACEPOINTS */
126 #define DECLARE_TRACE(name, proto, args) \
127 static inline void trace_##name(proto) \
128 { } \
129 static inline void _trace_##name(proto) \
130 { } \
131 static inline int register_trace_##name(void (*probe)(proto)) \
132 { \
133 return -ENOSYS; \
134 } \
135 static inline int unregister_trace_##name(void (*probe)(proto)) \
136 { \
137 return -ENOSYS; \
138 }
139
140 #define DEFINE_TRACE(name)
141 #define EXPORT_TRACEPOINT_SYMBOL_GPL(name)
142 #define EXPORT_TRACEPOINT_SYMBOL(name)
143
144 static inline void tracepoint_update_probe_range(struct tracepoint *begin,
145 struct tracepoint *end)
146 { }
147 #endif /* CONFIG_TRACEPOINTS */
148
149 /*
150 * Connect a probe to a tracepoint.
151 * Internal API, should not be used directly.
152 */
153 extern int tracepoint_probe_register(const char *name, void *probe);
154
155 /*
156 * Disconnect a probe from a tracepoint.
157 * Internal API, should not be used directly.
158 */
159 extern int tracepoint_probe_unregister(const char *name, void *probe);
160
161 extern int tracepoint_probe_register_noupdate(const char *name, void *probe);
162 extern int tracepoint_probe_unregister_noupdate(const char *name, void *probe);
163 extern void tracepoint_probe_update_all(void);
164
165 struct tracepoint_iter {
166 //ust// struct module *module;
167 struct tracepoint_lib *lib;
168 struct tracepoint *tracepoint;
169 };
170
171 extern void tracepoint_iter_start(struct tracepoint_iter *iter);
172 extern void tracepoint_iter_next(struct tracepoint_iter *iter);
173 extern void tracepoint_iter_stop(struct tracepoint_iter *iter);
174 extern void tracepoint_iter_reset(struct tracepoint_iter *iter);
175 extern int tracepoint_get_iter_range(struct tracepoint **tracepoint,
176 struct tracepoint *begin, struct tracepoint *end);
177
178 /*
179 * tracepoint_synchronize_unregister must be called between the last tracepoint
180 * probe unregistration and the end of module exit to make sure there is no
181 * caller executing a probe when it is freed.
182 */
183 static inline void tracepoint_synchronize_unregister(void)
184 {
185 //ust// synchronize_sched();
186 }
187
188 struct tracepoint_lib {
189 struct tracepoint *tracepoints_start;
190 int tracepoints_count;
191 struct list_head list;
192 };
193
194 extern int tracepoint_register_lib(struct tracepoint *tracepoints_start,
195 int tracepoints_count);
196 extern int tracepoint_unregister_lib(struct tracepoint *tracepoints_start);
197
198 #define TRACEPOINT_LIB \
199 extern struct tracepoint __start___tracepoints[] __attribute__((weak, visibility("hidden"))); \
200 extern struct tracepoint __stop___tracepoints[] __attribute__((weak, visibility("hidden"))); \
201 static void __attribute__((constructor)) __tracepoints__init(void) \
202 { \
203 tracepoint_register_lib(__start___tracepoints, \
204 (((long)__stop___tracepoints)-((long)__start___tracepoints))/sizeof(struct tracepoint)); \
205 } \
206 \
207 static void __attribute__((destructor)) __tracepoints__destroy(void) \
208 { \
209 tracepoint_unregister_lib(__start___tracepoints); \
210 }
211
212
213 #ifndef TRACE_EVENT
214 /*
215 * For use with the TRACE_EVENT macro:
216 *
217 * We define a tracepoint, its arguments, its printf format
218 * and its 'fast binary record' layout.
219 *
220 * Firstly, name your tracepoint via TRACE_EVENT(name : the
221 * 'subsystem_event' notation is fine.
222 *
223 * Think about this whole construct as the
224 * 'trace_sched_switch() function' from now on.
225 *
226 *
227 * TRACE_EVENT(sched_switch,
228 *
229 * *
230 * * A function has a regular function arguments
231 * * prototype, declare it via TP_PROTO():
232 * *
233 *
234 * TP_PROTO(struct rq *rq, struct task_struct *prev,
235 * struct task_struct *next),
236 *
237 * *
238 * * Define the call signature of the 'function'.
239 * * (Design sidenote: we use this instead of a
240 * * TP_PROTO1/TP_PROTO2/TP_PROTO3 ugliness.)
241 * *
242 *
243 * TP_ARGS(rq, prev, next),
244 *
245 * *
246 * * Fast binary tracing: define the trace record via
247 * * TP_STRUCT__entry(). You can think about it like a
248 * * regular C structure local variable definition.
249 * *
250 * * This is how the trace record is structured and will
251 * * be saved into the ring buffer. These are the fields
252 * * that will be exposed to readers.
253 * *
254 * * The declared 'local variable' is called '__entry'
255 * *
256 * * __field(pid_t, prev_prid) is equivalent to a standard declariton:
257 * *
258 * * pid_t prev_pid;
259 * *
260 * * __array(char, prev_comm, TASK_COMM_LEN) is equivalent to:
261 * *
262 * * char prev_comm[TASK_COMM_LEN];
263 * *
264 *
265 * TP_STRUCT__entry(
266 * __array( char, prev_comm, TASK_COMM_LEN )
267 * __field( pid_t, prev_pid )
268 * __field( int, prev_prio )
269 * __array( char, next_comm, TASK_COMM_LEN )
270 * __field( pid_t, next_pid )
271 * __field( int, next_prio )
272 * ),
273 *
274 * *
275 * * Assign the entry into the trace record, by embedding
276 * * a full C statement block into TP_fast_assign(). You
277 * * can refer to the trace record as '__entry' -
278 * * otherwise you can put arbitrary C code in here.
279 * *
280 * * Note: this C code will execute every time a trace event
281 * * happens, on an active tracepoint.
282 * *
283 *
284 * TP_fast_assign(
285 * memcpy(__entry->next_comm, next->comm, TASK_COMM_LEN);
286 * __entry->prev_pid = prev->pid;
287 * __entry->prev_prio = prev->prio;
288 * memcpy(__entry->prev_comm, prev->comm, TASK_COMM_LEN);
289 * __entry->next_pid = next->pid;
290 * __entry->next_prio = next->prio;
291 * )
292 *
293 * *
294 * * Formatted output of a trace record via TP_printf().
295 * * This is how the tracepoint will appear under debugging
296 * * of tracepoints.
297 * *
298 * * (raw-binary tracing wont actually perform this step.)
299 * *
300 *
301 * TP_printf("task %s:%d [%d] ==> %s:%d [%d]",
302 * __entry->prev_comm, __entry->prev_pid, __entry->prev_prio,
303 * __entry->next_comm, __entry->next_pid, __entry->next_prio),
304 *
305 * );
306 *
307 * This macro construct is thus used for the regular printf format
308 * tracing setup.
309 *
310 * A set of (un)registration functions can be passed to the variant
311 * TRACE_EVENT_FN to perform any (un)registration work.
312 */
313
314 #define DECLARE_TRACE_EVENT_CLASS(name, proto, args, tstruct, assign, print)
315 #define DEFINE_TRACE_EVENT(template, name, proto, args) \
316 DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
317 #define DEFINE_TRACE_EVENT_PRINT(template, name, proto, args, print) \
318 DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
319
320 #define TRACE_EVENT(name, proto, args, struct, assign, print) \
321 DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
322 #define TRACE_EVENT_FN(name, proto, args, struct, \
323 assign, print, reg, unreg) \
324 DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
325
326 #endif /* ifdef TRACE_EVENT (see note above) */
327
328
329 #endif /* _UST_TRACEPOINT_H */
This page took 0.036652 seconds and 5 git commands to generate.