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