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 36 #define _DPRINTK_FLAGS_INCL_ANY \ 37 (_DPRINTK_FLAGS_INCL_MODNAME | _DPRINTK_FLAGS_INCL_FUNCNAME |\ 38 _DPRINTK_FLAGS_INCL_LINENO | _DPRINTK_FLAGS_INCL_TID) 39 40 #if defined DEBUG 41 #define _DPRINTK_FLAGS_DEFAULT _DPRINTK_FLAGS_PRINT 42 #else 43 #define _DPRINTK_FLAGS_DEFAULT 0 44 #endif 45 unsigned int flags:8; 46 #ifdef CONFIG_JUMP_LABEL 47 union { 48 struct static_key_true dd_key_true; 49 struct static_key_false dd_key_false; 50 } key; 51 #endif 52 } __attribute__((aligned(8))); 53 54 /* encapsulate linker provided built-in (or module) dyndbg data */ 55 struct _ddebug_info { 56 struct _ddebug *descs; 57 unsigned int num_descs; 58 }; 59 60 #if defined(CONFIG_DYNAMIC_DEBUG_CORE) 61 62 int ddebug_add_module(struct _ddebug_info *dyndbg, const char *modname); 63 64 extern int ddebug_remove_module(const char *mod_name); 65 extern __printf(2, 3) 66 void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...); 67 68 extern int ddebug_dyndbg_module_param_cb(char *param, char *val, 69 const char *modname); 70 71 struct device; 72 73 extern __printf(3, 4) 74 void __dynamic_dev_dbg(struct _ddebug *descriptor, const struct device *dev, 75 const char *fmt, ...); 76 77 struct net_device; 78 79 extern __printf(3, 4) 80 void __dynamic_netdev_dbg(struct _ddebug *descriptor, 81 const struct net_device *dev, 82 const char *fmt, ...); 83 84 struct ib_device; 85 86 extern __printf(3, 4) 87 void __dynamic_ibdev_dbg(struct _ddebug *descriptor, 88 const struct ib_device *ibdev, 89 const char *fmt, ...); 90 91 #define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \ 92 static struct _ddebug __aligned(8) \ 93 __section("__dyndbg") name = { \ 94 .modname = KBUILD_MODNAME, \ 95 .function = __func__, \ 96 .filename = __FILE__, \ 97 .format = (fmt), \ 98 .lineno = __LINE__, \ 99 .flags = _DPRINTK_FLAGS_DEFAULT, \ 100 _DPRINTK_KEY_INIT \ 101 } 102 103 #ifdef CONFIG_JUMP_LABEL 104 105 #ifdef DEBUG 106 107 #define _DPRINTK_KEY_INIT .key.dd_key_true = (STATIC_KEY_TRUE_INIT) 108 109 #define DYNAMIC_DEBUG_BRANCH(descriptor) \ 110 static_branch_likely(&descriptor.key.dd_key_true) 111 #else 112 #define _DPRINTK_KEY_INIT .key.dd_key_false = (STATIC_KEY_FALSE_INIT) 113 114 #define DYNAMIC_DEBUG_BRANCH(descriptor) \ 115 static_branch_unlikely(&descriptor.key.dd_key_false) 116 #endif 117 118 #else /* !CONFIG_JUMP_LABEL */ 119 120 #define _DPRINTK_KEY_INIT 121 122 #ifdef DEBUG 123 #define DYNAMIC_DEBUG_BRANCH(descriptor) \ 124 likely(descriptor.flags & _DPRINTK_FLAGS_PRINT) 125 #else 126 #define DYNAMIC_DEBUG_BRANCH(descriptor) \ 127 unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT) 128 #endif 129 130 #endif /* CONFIG_JUMP_LABEL */ 131 132 #define __dynamic_func_call(id, fmt, func, ...) do { \ 133 DEFINE_DYNAMIC_DEBUG_METADATA(id, fmt); \ 134 if (DYNAMIC_DEBUG_BRANCH(id)) \ 135 func(&id, ##__VA_ARGS__); \ 136 } while (0) 137 138 #define __dynamic_func_call_no_desc(id, fmt, func, ...) do { \ 139 DEFINE_DYNAMIC_DEBUG_METADATA(id, fmt); \ 140 if (DYNAMIC_DEBUG_BRANCH(id)) \ 141 func(__VA_ARGS__); \ 142 } while (0) 143 144 /* 145 * "Factory macro" for generating a call to func, guarded by a 146 * DYNAMIC_DEBUG_BRANCH. The dynamic debug descriptor will be 147 * initialized using the fmt argument. The function will be called with 148 * the address of the descriptor as first argument, followed by all 149 * the varargs. Note that fmt is repeated in invocations of this 150 * macro. 151 */ 152 #define _dynamic_func_call(fmt, func, ...) \ 153 __dynamic_func_call(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__) 154 /* 155 * A variant that does the same, except that the descriptor is not 156 * passed as the first argument to the function; it is only called 157 * with precisely the macro's varargs. 158 */ 159 #define _dynamic_func_call_no_desc(fmt, func, ...) \ 160 __dynamic_func_call_no_desc(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__) 161 162 #define dynamic_pr_debug(fmt, ...) \ 163 _dynamic_func_call(fmt, __dynamic_pr_debug, \ 164 pr_fmt(fmt), ##__VA_ARGS__) 165 166 #define dynamic_dev_dbg(dev, fmt, ...) \ 167 _dynamic_func_call(fmt,__dynamic_dev_dbg, \ 168 dev, fmt, ##__VA_ARGS__) 169 170 #define dynamic_netdev_dbg(dev, fmt, ...) \ 171 _dynamic_func_call(fmt, __dynamic_netdev_dbg, \ 172 dev, fmt, ##__VA_ARGS__) 173 174 #define dynamic_ibdev_dbg(dev, fmt, ...) \ 175 _dynamic_func_call(fmt, __dynamic_ibdev_dbg, \ 176 dev, fmt, ##__VA_ARGS__) 177 178 #define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \ 179 groupsize, buf, len, ascii) \ 180 _dynamic_func_call_no_desc(__builtin_constant_p(prefix_str) ? prefix_str : "hexdump", \ 181 print_hex_dump, \ 182 KERN_DEBUG, prefix_str, prefix_type, \ 183 rowsize, groupsize, buf, len, ascii) 184 185 #else /* !CONFIG_DYNAMIC_DEBUG_CORE */ 186 187 #include <linux/string.h> 188 #include <linux/errno.h> 189 #include <linux/printk.h> 190 191 static inline int ddebug_add_module(struct _ddebug_info *dinfo, const char *modname) 192 { 193 return 0; 194 } 195 196 static inline int ddebug_remove_module(const char *mod) 197 { 198 return 0; 199 } 200 201 static inline int ddebug_dyndbg_module_param_cb(char *param, char *val, 202 const char *modname) 203 { 204 if (!strcmp(param, "dyndbg")) { 205 /* avoid pr_warn(), which wants pr_fmt() fully defined */ 206 printk(KERN_WARNING "dyndbg param is supported only in " 207 "CONFIG_DYNAMIC_DEBUG builds\n"); 208 return 0; /* allow and ignore */ 209 } 210 return -EINVAL; 211 } 212 213 #define dynamic_pr_debug(fmt, ...) \ 214 do { if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); } while (0) 215 #define dynamic_dev_dbg(dev, fmt, ...) \ 216 do { if (0) dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); } while (0) 217 #define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \ 218 groupsize, buf, len, ascii) \ 219 do { if (0) \ 220 print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, \ 221 rowsize, groupsize, buf, len, ascii); \ 222 } while (0) 223 224 #endif /* !CONFIG_DYNAMIC_DEBUG_CORE */ 225 226 #endif 227