1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _DYNAMIC_DEBUG_H 3 #define _DYNAMIC_DEBUG_H 4 5 #if defined(CONFIG_JUMP_LABEL) 6 #include <linux/jump_label.h> 7 #endif 8 9 /* 10 * An instance of this structure is created in a special 11 * ELF section at every dynamic debug callsite. At runtime, 12 * the special section is treated as an array of these. 13 */ 14 struct _ddebug { 15 /* 16 * These fields are used to drive the user interface 17 * for selecting and displaying debug callsites. 18 */ 19 const char *modname; 20 const char *function; 21 const char *filename; 22 const char *format; 23 unsigned int lineno:18; 24 /* 25 * The flags field controls the behaviour at the callsite. 26 * The bits here are changed dynamically when the user 27 * writes commands to <debugfs>/dynamic_debug/control 28 */ 29 #define _DPRINTK_FLAGS_NONE 0 30 #define _DPRINTK_FLAGS_PRINT (1<<0) /* printk() a message using the format */ 31 #define _DPRINTK_FLAGS_INCL_MODNAME (1<<1) 32 #define _DPRINTK_FLAGS_INCL_FUNCNAME (1<<2) 33 #define _DPRINTK_FLAGS_INCL_LINENO (1<<3) 34 #define _DPRINTK_FLAGS_INCL_TID (1<<4) 35 #if defined DEBUG 36 #define _DPRINTK_FLAGS_DEFAULT _DPRINTK_FLAGS_PRINT 37 #else 38 #define _DPRINTK_FLAGS_DEFAULT 0 39 #endif 40 unsigned int flags:8; 41 #ifdef CONFIG_JUMP_LABEL 42 union { 43 struct static_key_true dd_key_true; 44 struct static_key_false dd_key_false; 45 } key; 46 #endif 47 } __attribute__((aligned(8))); 48 49 50 int ddebug_add_module(struct _ddebug *tab, unsigned int n, 51 const char *modname); 52 53 #if defined(CONFIG_DYNAMIC_DEBUG) 54 extern int ddebug_remove_module(const char *mod_name); 55 extern __printf(2, 3) 56 void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...); 57 58 extern int ddebug_dyndbg_module_param_cb(char *param, char *val, 59 const char *modname); 60 61 struct device; 62 63 extern __printf(3, 4) 64 void __dynamic_dev_dbg(struct _ddebug *descriptor, const struct device *dev, 65 const char *fmt, ...); 66 67 struct net_device; 68 69 extern __printf(3, 4) 70 void __dynamic_netdev_dbg(struct _ddebug *descriptor, 71 const struct net_device *dev, 72 const char *fmt, ...); 73 74 #define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \ 75 static struct _ddebug __aligned(8) \ 76 __attribute__((section("__verbose"))) name = { \ 77 .modname = KBUILD_MODNAME, \ 78 .function = __func__, \ 79 .filename = __FILE__, \ 80 .format = (fmt), \ 81 .lineno = __LINE__, \ 82 .flags = _DPRINTK_FLAGS_DEFAULT, \ 83 _DPRINTK_KEY_INIT \ 84 } 85 86 #ifdef CONFIG_JUMP_LABEL 87 88 #ifdef DEBUG 89 90 #define _DPRINTK_KEY_INIT .key.dd_key_true = (STATIC_KEY_TRUE_INIT) 91 92 #define DYNAMIC_DEBUG_BRANCH(descriptor) \ 93 static_branch_likely(&descriptor.key.dd_key_true) 94 #else 95 #define _DPRINTK_KEY_INIT .key.dd_key_false = (STATIC_KEY_FALSE_INIT) 96 97 #define DYNAMIC_DEBUG_BRANCH(descriptor) \ 98 static_branch_unlikely(&descriptor.key.dd_key_false) 99 #endif 100 101 #else /* !HAVE_JUMP_LABEL */ 102 103 #define _DPRINTK_KEY_INIT 104 105 #ifdef DEBUG 106 #define DYNAMIC_DEBUG_BRANCH(descriptor) \ 107 likely(descriptor.flags & _DPRINTK_FLAGS_PRINT) 108 #else 109 #define DYNAMIC_DEBUG_BRANCH(descriptor) \ 110 unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT) 111 #endif 112 113 #endif 114 115 #define dynamic_pr_debug(fmt, ...) \ 116 do { \ 117 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \ 118 if (DYNAMIC_DEBUG_BRANCH(descriptor)) \ 119 __dynamic_pr_debug(&descriptor, pr_fmt(fmt), \ 120 ##__VA_ARGS__); \ 121 } while (0) 122 123 #define dynamic_dev_dbg(dev, fmt, ...) \ 124 do { \ 125 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \ 126 if (DYNAMIC_DEBUG_BRANCH(descriptor)) \ 127 __dynamic_dev_dbg(&descriptor, dev, fmt, \ 128 ##__VA_ARGS__); \ 129 } while (0) 130 131 #define dynamic_netdev_dbg(dev, fmt, ...) \ 132 do { \ 133 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \ 134 if (DYNAMIC_DEBUG_BRANCH(descriptor)) \ 135 __dynamic_netdev_dbg(&descriptor, dev, fmt, \ 136 ##__VA_ARGS__); \ 137 } while (0) 138 139 #define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \ 140 groupsize, buf, len, ascii) \ 141 do { \ 142 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, \ 143 __builtin_constant_p(prefix_str) ? prefix_str : "hexdump");\ 144 if (DYNAMIC_DEBUG_BRANCH(descriptor)) \ 145 print_hex_dump(KERN_DEBUG, prefix_str, \ 146 prefix_type, rowsize, groupsize, \ 147 buf, len, ascii); \ 148 } while (0) 149 150 #else 151 152 #include <linux/string.h> 153 #include <linux/errno.h> 154 155 static inline int ddebug_remove_module(const char *mod) 156 { 157 return 0; 158 } 159 160 static inline int ddebug_dyndbg_module_param_cb(char *param, char *val, 161 const char *modname) 162 { 163 if (strstr(param, "dyndbg")) { 164 /* avoid pr_warn(), which wants pr_fmt() fully defined */ 165 printk(KERN_WARNING "dyndbg param is supported only in " 166 "CONFIG_DYNAMIC_DEBUG builds\n"); 167 return 0; /* allow and ignore */ 168 } 169 return -EINVAL; 170 } 171 172 #define dynamic_pr_debug(fmt, ...) \ 173 do { if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); } while (0) 174 #define dynamic_dev_dbg(dev, fmt, ...) \ 175 do { if (0) dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); } while (0) 176 #endif 177 178 #endif 179