linux/include/linux/tracepoint.h
<<
>>
Prefs
   1#ifndef _LINUX_TRACEPOINT_H
   2#define _LINUX_TRACEPOINT_H
   3
   4/*
   5 * Kernel Tracepoint API.
   6 *
   7 * See Documentation/trace/tracepoints.txt.
   8 *
   9 * (C) Copyright 2008 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
  10 *
  11 * Heavily inspired from the Linux Kernel Markers.
  12 *
  13 * This file is released under the GPLv2.
  14 * See the file COPYING for more details.
  15 */
  16
  17#include <linux/errno.h>
  18#include <linux/types.h>
  19#include <linux/rcupdate.h>
  20#include <linux/static_key.h>
  21
  22struct module;
  23struct tracepoint;
  24
  25struct tracepoint_func {
  26        void *func;
  27        void *data;
  28};
  29
  30struct tracepoint {
  31        const char *name;               /* Tracepoint name */
  32        struct static_key key;
  33        void (*regfunc)(void);
  34        void (*unregfunc)(void);
  35        struct tracepoint_func __rcu *funcs;
  36};
  37
  38struct trace_enum_map {
  39        const char              *system;
  40        const char              *enum_string;
  41        unsigned long           enum_value;
  42};
  43
  44/*
  45 * Connect a probe to a tracepoint.
  46 * Internal API, should not be used directly.
  47 */
  48extern int tracepoint_probe_register(const char *name, void *probe, void *data);
  49
  50/*
  51 * Disconnect a probe from a tracepoint.
  52 * Internal API, should not be used directly.
  53 */
  54extern int
  55tracepoint_probe_unregister(const char *name, void *probe, void *data);
  56
  57extern int tracepoint_probe_register_noupdate(const char *name, void *probe,
  58                                              void *data);
  59extern int tracepoint_probe_unregister_noupdate(const char *name, void *probe,
  60                                                void *data);
  61extern void tracepoint_probe_update_all(void);
  62
  63#ifdef CONFIG_MODULES
  64struct tp_module {
  65        struct list_head list;
  66        unsigned int num_tracepoints;
  67        struct tracepoint * const *tracepoints_ptrs;
  68};
  69bool trace_module_has_bad_taint(struct module *mod);
  70#else
  71static inline bool trace_module_has_bad_taint(struct module *mod)
  72{
  73        return false;
  74}
  75#endif /* CONFIG_MODULES */
  76
  77struct tracepoint_iter {
  78#ifdef CONFIG_MODULES
  79        struct tp_module *module;
  80#endif /* CONFIG_MODULES */
  81        struct tracepoint * const *tracepoint;
  82};
  83
  84extern void tracepoint_iter_start(struct tracepoint_iter *iter);
  85extern void tracepoint_iter_next(struct tracepoint_iter *iter);
  86extern void tracepoint_iter_stop(struct tracepoint_iter *iter);
  87extern void tracepoint_iter_reset(struct tracepoint_iter *iter);
  88
  89/*
  90 * tracepoint_synchronize_unregister must be called between the last tracepoint
  91 * probe unregistration and the end of module exit to make sure there is no
  92 * caller executing a probe when it is freed.
  93 */
  94static inline void tracepoint_synchronize_unregister(void)
  95{
  96        synchronize_sched();
  97}
  98
  99#define PARAMS(args...) args
 100
 101#define TRACE_DEFINE_ENUM(x)
 102
 103#endif /* _LINUX_TRACEPOINT_H */
 104
 105/*
 106 * Note: we keep the TRACE_EVENT and DECLARE_TRACE outside the include
 107 *  file ifdef protection.
 108 *  This is due to the way trace events work. If a file includes two
 109 *  trace event headers under one "CREATE_TRACE_POINTS" the first include
 110 *  will override the TRACE_EVENT and break the second include.
 111 */
 112
 113#ifndef DECLARE_TRACE
 114
 115#define TP_PROTO(args...)       args
 116#define TP_ARGS(args...)        args
 117#define TP_CONDITION(args...)   args
 118
 119#ifdef CONFIG_TRACEPOINTS
 120
 121/*
 122 * it_func[0] is never NULL because there is at least one element in the array
 123 * when the array itself is non NULL.
 124 *
 125 * Note, the proto and args passed in includes "__data" as the first parameter.
 126 * The reason for this is to handle the "void" prototype. If a tracepoint
 127 * has a "void" prototype, then it is invalid to declare a function
 128 * as "(void *, void)". The DECLARE_TRACE_NOARGS() will pass in just
 129 * "void *data", where as the DECLARE_TRACE() will pass in "void *data, proto".
 130 */
 131#define __DO_TRACE(tp, proto, args, cond, prercu, postrcu)              \
 132        do {                                                            \
 133                struct tracepoint_func *it_func_ptr;                    \
 134                void *it_func;                                          \
 135                void *__data;                                           \
 136                                                                        \
 137                if (!(cond))                                            \
 138                        return;                                         \
 139                prercu;                                                 \
 140                rcu_read_lock_sched_notrace();                          \
 141                it_func_ptr = rcu_dereference_sched((tp)->funcs);       \
 142                if (it_func_ptr) {                                      \
 143                        do {                                            \
 144                                it_func = (it_func_ptr)->func;          \
 145                                __data = (it_func_ptr)->data;           \
 146                                ((void(*)(proto))(it_func))(args);      \
 147                        } while ((++it_func_ptr)->func);                \
 148                }                                                       \
 149                rcu_read_unlock_sched_notrace();                        \
 150                postrcu;                                                \
 151        } while (0)
 152
 153#ifndef MODULE
 154#define __DECLARE_TRACE_RCU(name, proto, args, cond, data_proto, data_args)     \
 155        static inline void trace_##name##_rcuidle(proto)                \
 156        {                                                               \
 157                if (static_key_false(&__tracepoint_##name.key))         \
 158                        __DO_TRACE(&__tracepoint_##name,                \
 159                                TP_PROTO(data_proto),                   \
 160                                TP_ARGS(data_args),                     \
 161                                TP_CONDITION(cond),                     \
 162                                rcu_irq_enter(),                        \
 163                                rcu_irq_exit());                        \
 164        }
 165#else
 166#define __DECLARE_TRACE_RCU(name, proto, args, cond, data_proto, data_args)
 167#endif
 168
 169/*
 170 * Make sure the alignment of the structure in the __tracepoints section will
 171 * not add unwanted padding between the beginning of the section and the
 172 * structure. Force alignment to the same alignment as the section start.
 173 */
 174#define __DECLARE_TRACE(name, proto, args, cond, data_proto, data_args) \
 175        extern struct tracepoint __tracepoint_##name;                   \
 176        static inline void trace_##name(proto)                          \
 177        {                                                               \
 178                if (static_key_false(&__tracepoint_##name.key))         \
 179                        __DO_TRACE(&__tracepoint_##name,                \
 180                                TP_PROTO(data_proto),                   \
 181                                TP_ARGS(data_args),                     \
 182                                TP_CONDITION(cond),,);                  \
 183        }                                                               \
 184        __DECLARE_TRACE_RCU(name, PARAMS(proto), PARAMS(args),          \
 185                PARAMS(cond), PARAMS(data_proto), PARAMS(data_args))    \
 186        static inline int                                               \
 187        register_trace_##name(void (*probe)(data_proto), void *data)    \
 188        {                                                               \
 189                return tracepoint_probe_register(#name, (void *)probe,  \
 190                                                 data);                 \
 191        }                                                               \
 192        static inline int                                               \
 193        unregister_trace_##name(void (*probe)(data_proto), void *data)  \
 194        {                                                               \
 195                return tracepoint_probe_unregister(#name, (void *)probe, \
 196                                                   data);               \
 197        }                                                               \
 198        static inline void                                              \
 199        check_trace_callback_type_##name(void (*cb)(data_proto))        \
 200        {                                                               \
 201        }                                                               \
 202        static inline bool                                              \
 203        trace_##name##_enabled(void)                                    \
 204        {                                                               \
 205                return static_key_false(&__tracepoint_##name.key);      \
 206        }
 207
 208/*
 209 * We have no guarantee that gcc and the linker won't up-align the tracepoint
 210 * structures, so we create an array of pointers that will be used for iteration
 211 * on the tracepoints.
 212 */
 213#define DEFINE_TRACE_FN(name, reg, unreg)                                \
 214        static const char __tpstrtab_##name[]                            \
 215        __attribute__((section("__tracepoints_strings"))) = #name;       \
 216        struct tracepoint __tracepoint_##name                            \
 217        __attribute__((section("__tracepoints"))) =                      \
 218                { __tpstrtab_##name, STATIC_KEY_INIT_FALSE, reg, unreg, NULL };\
 219        static struct tracepoint * const __tracepoint_ptr_##name __used  \
 220        __attribute__((section("__tracepoints_ptrs"))) =                 \
 221                &__tracepoint_##name;
 222
 223#define DEFINE_TRACE(name)                                              \
 224        DEFINE_TRACE_FN(name, NULL, NULL);
 225
 226#define EXPORT_TRACEPOINT_SYMBOL_GPL(name)                              \
 227        EXPORT_SYMBOL_GPL(__tracepoint_##name)
 228#define EXPORT_TRACEPOINT_SYMBOL(name)                                  \
 229        EXPORT_SYMBOL(__tracepoint_##name)
 230
 231#else /* !CONFIG_TRACEPOINTS */
 232#define __DECLARE_TRACE(name, proto, args, cond, data_proto, data_args) \
 233        static inline void trace_##name(proto)                          \
 234        { }                                                             \
 235        static inline void trace_##name##_rcuidle(proto)                \
 236        { }                                                             \
 237        static inline int                                               \
 238        register_trace_##name(void (*probe)(data_proto),                \
 239                              void *data)                               \
 240        {                                                               \
 241                return -ENOSYS;                                         \
 242        }                                                               \
 243        static inline int                                               \
 244        unregister_trace_##name(void (*probe)(data_proto),              \
 245                                void *data)                             \
 246        {                                                               \
 247                return -ENOSYS;                                         \
 248        }                                                               \
 249        static inline void check_trace_callback_type_##name(void (*cb)(data_proto)) \
 250        {                                                               \
 251        }                                                               \
 252        static inline bool                                              \
 253        trace_##name##_enabled(void)                                    \
 254        {                                                               \
 255                return false;                                           \
 256        }
 257
 258#define DEFINE_TRACE_FN(name, reg, unreg)
 259#define DEFINE_TRACE(name)
 260#define EXPORT_TRACEPOINT_SYMBOL_GPL(name)
 261#define EXPORT_TRACEPOINT_SYMBOL(name)
 262
 263#endif /* CONFIG_TRACEPOINTS */
 264
 265/*
 266 * The need for the DECLARE_TRACE_NOARGS() is to handle the prototype
 267 * (void). "void" is a special value in a function prototype and can
 268 * not be combined with other arguments. Since the DECLARE_TRACE()
 269 * macro adds a data element at the beginning of the prototype,
 270 * we need a way to differentiate "(void *data, proto)" from
 271 * "(void *data, void)". The second prototype is invalid.
 272 *
 273 * DECLARE_TRACE_NOARGS() passes "void" as the tracepoint prototype
 274 * and "void *__data" as the callback prototype.
 275 *
 276 * DECLARE_TRACE() passes "proto" as the tracepoint protoype and
 277 * "void *__data, proto" as the callback prototype.
 278 */
 279#define DECLARE_TRACE_NOARGS(name)                                      \
 280                __DECLARE_TRACE(name, void, , 1, void *__data, __data)
 281
 282#define DECLARE_TRACE(name, proto, args)                                \
 283                __DECLARE_TRACE(name, PARAMS(proto), PARAMS(args), 1,   \
 284                                PARAMS(void *__data, proto),            \
 285                                PARAMS(__data, args))
 286
 287#define DECLARE_TRACE_CONDITION(name, proto, args, cond)                \
 288        __DECLARE_TRACE(name, PARAMS(proto), PARAMS(args), PARAMS(cond), \
 289                        PARAMS(void *__data, proto),                    \
 290                        PARAMS(__data, args))
 291
 292#define TRACE_EVENT_FLAGS(event, flag)
 293
 294#endif /* DECLARE_TRACE */
 295
 296#ifndef TRACE_EVENT
 297/*
 298 * For use with the TRACE_EVENT macro:
 299 *
 300 * We define a tracepoint, its arguments, its printk format
 301 * and its 'fast binay record' layout.
 302 *
 303 * Firstly, name your tracepoint via TRACE_EVENT(name : the
 304 * 'subsystem_event' notation is fine.
 305 *
 306 * Think about this whole construct as the
 307 * 'trace_sched_switch() function' from now on.
 308 *
 309 *
 310 *  TRACE_EVENT(sched_switch,
 311 *
 312 *      *
 313 *      * A function has a regular function arguments
 314 *      * prototype, declare it via TP_PROTO():
 315 *      *
 316 *
 317 *      TP_PROTO(struct rq *rq, struct task_struct *prev,
 318 *               struct task_struct *next),
 319 *
 320 *      *
 321 *      * Define the call signature of the 'function'.
 322 *      * (Design sidenote: we use this instead of a
 323 *      *  TP_PROTO1/TP_PROTO2/TP_PROTO3 ugliness.)
 324 *      *
 325 *
 326 *      TP_ARGS(rq, prev, next),
 327 *
 328 *      *
 329 *      * Fast binary tracing: define the trace record via
 330 *      * TP_STRUCT__entry(). You can think about it like a
 331 *      * regular C structure local variable definition.
 332 *      *
 333 *      * This is how the trace record is structured and will
 334 *      * be saved into the ring buffer. These are the fields
 335 *      * that will be exposed to user-space in
 336 *      * /sys/kernel/debug/tracing/events/<*>/format.
 337 *      *
 338 *      * The declared 'local variable' is called '__entry'
 339 *      *
 340 *      * __field(pid_t, prev_prid) is equivalent to a standard declariton:
 341 *      *
 342 *      *       pid_t   prev_pid;
 343 *      *
 344 *      * __array(char, prev_comm, TASK_COMM_LEN) is equivalent to:
 345 *      *
 346 *      *       char    prev_comm[TASK_COMM_LEN];
 347 *      *
 348 *
 349 *      TP_STRUCT__entry(
 350 *              __array(        char,   prev_comm,      TASK_COMM_LEN   )
 351 *              __field(        pid_t,  prev_pid                        )
 352 *              __field(        int,    prev_prio                       )
 353 *              __array(        char,   next_comm,      TASK_COMM_LEN   )
 354 *              __field(        pid_t,  next_pid                        )
 355 *              __field(        int,    next_prio                       )
 356 *      ),
 357 *
 358 *      *
 359 *      * Assign the entry into the trace record, by embedding
 360 *      * a full C statement block into TP_fast_assign(). You
 361 *      * can refer to the trace record as '__entry' -
 362 *      * otherwise you can put arbitrary C code in here.
 363 *      *
 364 *      * Note: this C code will execute every time a trace event
 365 *      * happens, on an active tracepoint.
 366 *      *
 367 *
 368 *      TP_fast_assign(
 369 *              memcpy(__entry->next_comm, next->comm, TASK_COMM_LEN);
 370 *              __entry->prev_pid       = prev->pid;
 371 *              __entry->prev_prio      = prev->prio;
 372 *              memcpy(__entry->prev_comm, prev->comm, TASK_COMM_LEN);
 373 *              __entry->next_pid       = next->pid;
 374 *              __entry->next_prio      = next->prio;
 375 *      ),
 376 *
 377 *      *
 378 *      * Formatted output of a trace record via TP_printk().
 379 *      * This is how the tracepoint will appear under ftrace
 380 *      * plugins that make use of this tracepoint.
 381 *      *
 382 *      * (raw-binary tracing wont actually perform this step.)
 383 *      *
 384 *
 385 *      TP_printk("task %s:%d [%d] ==> %s:%d [%d]",
 386 *              __entry->prev_comm, __entry->prev_pid, __entry->prev_prio,
 387 *              __entry->next_comm, __entry->next_pid, __entry->next_prio),
 388 *
 389 * );
 390 *
 391 * This macro construct is thus used for the regular printk format
 392 * tracing setup, it is used to construct a function pointer based
 393 * tracepoint callback (this is used by programmatic plugins and
 394 * can also by used by generic instrumentation like SystemTap), and
 395 * it is also used to expose a structured trace record in
 396 * /sys/kernel/debug/tracing/events/.
 397 *
 398 * A set of (un)registration functions can be passed to the variant
 399 * TRACE_EVENT_FN to perform any (un)registration work.
 400 */
 401
 402#define DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, print)
 403#define DEFINE_EVENT(template, name, proto, args)               \
 404        DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
 405#define DEFINE_EVENT_FN(template, name, proto, args, reg, unreg)\
 406        DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
 407#define DEFINE_EVENT_PRINT(template, name, proto, args, print)  \
 408        DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
 409#define DEFINE_EVENT_CONDITION(template, name, proto,           \
 410                               args, cond)                      \
 411        DECLARE_TRACE_CONDITION(name, PARAMS(proto),            \
 412                                PARAMS(args), PARAMS(cond))
 413
 414#define TRACE_EVENT(name, proto, args, struct, assign, print)   \
 415        DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
 416#define TRACE_EVENT_FN(name, proto, args, struct,               \
 417                assign, print, reg, unreg)                      \
 418        DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
 419#define TRACE_EVENT_CONDITION(name, proto, args, cond,          \
 420                              struct, assign, print)            \
 421        DECLARE_TRACE_CONDITION(name, PARAMS(proto),            \
 422                                PARAMS(args), PARAMS(cond))
 423
 424#define TRACE_EVENT_FLAGS(event, flag)
 425
 426#endif /* ifdef TRACE_EVENT (see note above) */
 427