Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _DYNAMIC_DEBUG_H
0003 #define _DYNAMIC_DEBUG_H
0004 
0005 #if defined(CONFIG_JUMP_LABEL)
0006 #include <linux/jump_label.h>
0007 #endif
0008 
0009 /*
0010  * An instance of this structure is created in a special
0011  * ELF section at every dynamic debug callsite.  At runtime,
0012  * the special section is treated as an array of these.
0013  */
0014 struct _ddebug {
0015     /*
0016      * These fields are used to drive the user interface
0017      * for selecting and displaying debug callsites.
0018      */
0019     const char *modname;
0020     const char *function;
0021     const char *filename;
0022     const char *format;
0023     unsigned int lineno:18;
0024     /*
0025      * The flags field controls the behaviour at the callsite.
0026      * The bits here are changed dynamically when the user
0027      * writes commands to <debugfs>/dynamic_debug/control
0028      */
0029 #define _DPRINTK_FLAGS_NONE 0
0030 #define _DPRINTK_FLAGS_PRINT    (1<<0) /* printk() a message using the format */
0031 #define _DPRINTK_FLAGS_INCL_MODNAME (1<<1)
0032 #define _DPRINTK_FLAGS_INCL_FUNCNAME    (1<<2)
0033 #define _DPRINTK_FLAGS_INCL_LINENO  (1<<3)
0034 #define _DPRINTK_FLAGS_INCL_TID     (1<<4)
0035 
0036 #define _DPRINTK_FLAGS_INCL_ANY     \
0037     (_DPRINTK_FLAGS_INCL_MODNAME | _DPRINTK_FLAGS_INCL_FUNCNAME |\
0038      _DPRINTK_FLAGS_INCL_LINENO  | _DPRINTK_FLAGS_INCL_TID)
0039 
0040 #if defined DEBUG
0041 #define _DPRINTK_FLAGS_DEFAULT _DPRINTK_FLAGS_PRINT
0042 #else
0043 #define _DPRINTK_FLAGS_DEFAULT 0
0044 #endif
0045     unsigned int flags:8;
0046 #ifdef CONFIG_JUMP_LABEL
0047     union {
0048         struct static_key_true dd_key_true;
0049         struct static_key_false dd_key_false;
0050     } key;
0051 #endif
0052 } __attribute__((aligned(8)));
0053 
0054 
0055 
0056 #if defined(CONFIG_DYNAMIC_DEBUG_CORE)
0057 
0058 /* exported for module authors to exercise >control */
0059 int dynamic_debug_exec_queries(const char *query, const char *modname);
0060 
0061 int ddebug_add_module(struct _ddebug *tab, unsigned int n,
0062                 const char *modname);
0063 extern int ddebug_remove_module(const char *mod_name);
0064 extern __printf(2, 3)
0065 void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...);
0066 
0067 extern int ddebug_dyndbg_module_param_cb(char *param, char *val,
0068                     const char *modname);
0069 
0070 struct device;
0071 
0072 extern __printf(3, 4)
0073 void __dynamic_dev_dbg(struct _ddebug *descriptor, const struct device *dev,
0074                const char *fmt, ...);
0075 
0076 struct net_device;
0077 
0078 extern __printf(3, 4)
0079 void __dynamic_netdev_dbg(struct _ddebug *descriptor,
0080               const struct net_device *dev,
0081               const char *fmt, ...);
0082 
0083 struct ib_device;
0084 
0085 extern __printf(3, 4)
0086 void __dynamic_ibdev_dbg(struct _ddebug *descriptor,
0087              const struct ib_device *ibdev,
0088              const char *fmt, ...);
0089 
0090 #define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt)        \
0091     static struct _ddebug  __aligned(8)         \
0092     __section("__dyndbg") name = {              \
0093         .modname = KBUILD_MODNAME,          \
0094         .function = __func__,               \
0095         .filename = __FILE__,               \
0096         .format = (fmt),                \
0097         .lineno = __LINE__,             \
0098         .flags = _DPRINTK_FLAGS_DEFAULT,        \
0099         _DPRINTK_KEY_INIT               \
0100     }
0101 
0102 #ifdef CONFIG_JUMP_LABEL
0103 
0104 #ifdef DEBUG
0105 
0106 #define _DPRINTK_KEY_INIT .key.dd_key_true = (STATIC_KEY_TRUE_INIT)
0107 
0108 #define DYNAMIC_DEBUG_BRANCH(descriptor) \
0109     static_branch_likely(&descriptor.key.dd_key_true)
0110 #else
0111 #define _DPRINTK_KEY_INIT .key.dd_key_false = (STATIC_KEY_FALSE_INIT)
0112 
0113 #define DYNAMIC_DEBUG_BRANCH(descriptor) \
0114     static_branch_unlikely(&descriptor.key.dd_key_false)
0115 #endif
0116 
0117 #else /* !CONFIG_JUMP_LABEL */
0118 
0119 #define _DPRINTK_KEY_INIT
0120 
0121 #ifdef DEBUG
0122 #define DYNAMIC_DEBUG_BRANCH(descriptor) \
0123     likely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
0124 #else
0125 #define DYNAMIC_DEBUG_BRANCH(descriptor) \
0126     unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
0127 #endif
0128 
0129 #endif /* CONFIG_JUMP_LABEL */
0130 
0131 #define __dynamic_func_call(id, fmt, func, ...) do {    \
0132     DEFINE_DYNAMIC_DEBUG_METADATA(id, fmt);     \
0133     if (DYNAMIC_DEBUG_BRANCH(id))           \
0134         func(&id, ##__VA_ARGS__);       \
0135 } while (0)
0136 
0137 #define __dynamic_func_call_no_desc(id, fmt, func, ...) do {    \
0138     DEFINE_DYNAMIC_DEBUG_METADATA(id, fmt);         \
0139     if (DYNAMIC_DEBUG_BRANCH(id))               \
0140         func(__VA_ARGS__);              \
0141 } while (0)
0142 
0143 /*
0144  * "Factory macro" for generating a call to func, guarded by a
0145  * DYNAMIC_DEBUG_BRANCH. The dynamic debug descriptor will be
0146  * initialized using the fmt argument. The function will be called with
0147  * the address of the descriptor as first argument, followed by all
0148  * the varargs. Note that fmt is repeated in invocations of this
0149  * macro.
0150  */
0151 #define _dynamic_func_call(fmt, func, ...)              \
0152     __dynamic_func_call(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__)
0153 /*
0154  * A variant that does the same, except that the descriptor is not
0155  * passed as the first argument to the function; it is only called
0156  * with precisely the macro's varargs.
0157  */
0158 #define _dynamic_func_call_no_desc(fmt, func, ...)  \
0159     __dynamic_func_call_no_desc(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__)
0160 
0161 #define dynamic_pr_debug(fmt, ...)              \
0162     _dynamic_func_call(fmt, __dynamic_pr_debug,     \
0163                pr_fmt(fmt), ##__VA_ARGS__)
0164 
0165 #define dynamic_dev_dbg(dev, fmt, ...)              \
0166     _dynamic_func_call(fmt,__dynamic_dev_dbg,       \
0167                dev, fmt, ##__VA_ARGS__)
0168 
0169 #define dynamic_netdev_dbg(dev, fmt, ...)           \
0170     _dynamic_func_call(fmt, __dynamic_netdev_dbg,       \
0171                dev, fmt, ##__VA_ARGS__)
0172 
0173 #define dynamic_ibdev_dbg(dev, fmt, ...)            \
0174     _dynamic_func_call(fmt, __dynamic_ibdev_dbg,        \
0175                dev, fmt, ##__VA_ARGS__)
0176 
0177 #define dynamic_hex_dump(prefix_str, prefix_type, rowsize,      \
0178              groupsize, buf, len, ascii)            \
0179     _dynamic_func_call_no_desc(__builtin_constant_p(prefix_str) ? prefix_str : "hexdump", \
0180                    print_hex_dump,          \
0181                    KERN_DEBUG, prefix_str, prefix_type, \
0182                    rowsize, groupsize, buf, len, ascii)
0183 
0184 #else /* !CONFIG_DYNAMIC_DEBUG_CORE */
0185 
0186 #include <linux/string.h>
0187 #include <linux/errno.h>
0188 #include <linux/printk.h>
0189 
0190 static inline int ddebug_add_module(struct _ddebug *tab, unsigned int n,
0191                     const char *modname)
0192 {
0193     return 0;
0194 }
0195 
0196 static inline int ddebug_remove_module(const char *mod)
0197 {
0198     return 0;
0199 }
0200 
0201 static inline int ddebug_dyndbg_module_param_cb(char *param, char *val,
0202                         const char *modname)
0203 {
0204     if (strstr(param, "dyndbg")) {
0205         /* avoid pr_warn(), which wants pr_fmt() fully defined */
0206         printk(KERN_WARNING "dyndbg param is supported only in "
0207             "CONFIG_DYNAMIC_DEBUG builds\n");
0208         return 0; /* allow and ignore */
0209     }
0210     return -EINVAL;
0211 }
0212 
0213 #define dynamic_pr_debug(fmt, ...)                  \
0214     do { if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); } while (0)
0215 #define dynamic_dev_dbg(dev, fmt, ...)                  \
0216     do { if (0) dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); } while (0)
0217 #define dynamic_hex_dump(prefix_str, prefix_type, rowsize,      \
0218              groupsize, buf, len, ascii)            \
0219     do { if (0)                         \
0220         print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, \
0221                 rowsize, groupsize, buf, len, ascii);   \
0222     } while (0)
0223 
0224 static inline int dynamic_debug_exec_queries(const char *query, const char *modname)
0225 {
0226     pr_warn("kernel not built with CONFIG_DYNAMIC_DEBUG_CORE\n");
0227     return 0;
0228 }
0229 
0230 #endif /* !CONFIG_DYNAMIC_DEBUG_CORE */
0231 
0232 #endif