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 #include <linux/build_bug.h> 10 11 /* 12 * An instance of this structure is created in a special 13 * ELF section at every dynamic debug callsite. At runtime, 14 * the special section is treated as an array of these. 15 */ 16 struct _ddebug { 17 /* 18 * These fields are used to drive the user interface 19 * for selecting and displaying debug callsites. 20 */ 21 const char *modname; 22 const char *function; 23 const char *filename; 24 const char *format; 25 unsigned int lineno:18; 26 #define CLS_BITS 6 27 unsigned int class_id:CLS_BITS; 28 #define _DPRINTK_CLASS_DFLT ((1 << CLS_BITS) - 1) 29 /* 30 * The flags field controls the behaviour at the callsite. 31 * The bits here are changed dynamically when the user 32 * writes commands to <debugfs>/dynamic_debug/control 33 */ 34 #define _DPRINTK_FLAGS_NONE 0 35 #define _DPRINTK_FLAGS_PRINT (1<<0) /* printk() a message using the format */ 36 #define _DPRINTK_FLAGS_INCL_MODNAME (1<<1) 37 #define _DPRINTK_FLAGS_INCL_FUNCNAME (1<<2) 38 #define _DPRINTK_FLAGS_INCL_LINENO (1<<3) 39 #define _DPRINTK_FLAGS_INCL_TID (1<<4) 40 41 #define _DPRINTK_FLAGS_INCL_ANY \ 42 (_DPRINTK_FLAGS_INCL_MODNAME | _DPRINTK_FLAGS_INCL_FUNCNAME |\ 43 _DPRINTK_FLAGS_INCL_LINENO | _DPRINTK_FLAGS_INCL_TID) 44 45 #if defined DEBUG 46 #define _DPRINTK_FLAGS_DEFAULT _DPRINTK_FLAGS_PRINT 47 #else 48 #define _DPRINTK_FLAGS_DEFAULT 0 49 #endif 50 unsigned int flags:8; 51 #ifdef CONFIG_JUMP_LABEL 52 union { 53 struct static_key_true dd_key_true; 54 struct static_key_false dd_key_false; 55 } key; 56 #endif 57 } __attribute__((aligned(8))); 58 59 /* encapsulate linker provided built-in (or module) dyndbg data */ 60 struct _ddebug_info { 61 struct _ddebug *descs; 62 unsigned int num_descs; 63 }; 64 65 #if defined(CONFIG_DYNAMIC_DEBUG_CORE) 66 67 int ddebug_add_module(struct _ddebug_info *dyndbg, const char *modname); 68 69 extern int ddebug_remove_module(const char *mod_name); 70 extern __printf(2, 3) 71 void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...); 72 73 extern int ddebug_dyndbg_module_param_cb(char *param, char *val, 74 const char *modname); 75 76 struct device; 77 78 extern __printf(3, 4) 79 void __dynamic_dev_dbg(struct _ddebug *descriptor, const struct device *dev, 80 const char *fmt, ...); 81 82 struct net_device; 83 84 extern __printf(3, 4) 85 void __dynamic_netdev_dbg(struct _ddebug *descriptor, 86 const struct net_device *dev, 87 const char *fmt, ...); 88 89 struct ib_device; 90 91 extern __printf(3, 4) 92 void __dynamic_ibdev_dbg(struct _ddebug *descriptor, 93 const struct ib_device *ibdev, 94 const char *fmt, ...); 95 96 #define DEFINE_DYNAMIC_DEBUG_METADATA_CLS(name, cls, fmt) \ 97 static struct _ddebug __aligned(8) \ 98 __section("__dyndbg") name = { \ 99 .modname = KBUILD_MODNAME, \ 100 .function = __func__, \ 101 .filename = __FILE__, \ 102 .format = (fmt), \ 103 .lineno = __LINE__, \ 104 .flags = _DPRINTK_FLAGS_DEFAULT, \ 105 .class_id = cls, \ 106 _DPRINTK_KEY_INIT \ 107 }; \ 108 BUILD_BUG_ON_MSG(cls > _DPRINTK_CLASS_DFLT, \ 109 "classid value overflow") 110 111 #define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \ 112 DEFINE_DYNAMIC_DEBUG_METADATA_CLS(name, _DPRINTK_CLASS_DFLT, fmt) 113 114 #ifdef CONFIG_JUMP_LABEL 115 116 #ifdef DEBUG 117 118 #define _DPRINTK_KEY_INIT .key.dd_key_true = (STATIC_KEY_TRUE_INIT) 119 120 #define DYNAMIC_DEBUG_BRANCH(descriptor) \ 121 static_branch_likely(&descriptor.key.dd_key_true) 122 #else 123 #define _DPRINTK_KEY_INIT .key.dd_key_false = (STATIC_KEY_FALSE_INIT) 124 125 #define DYNAMIC_DEBUG_BRANCH(descriptor) \ 126 static_branch_unlikely(&descriptor.key.dd_key_false) 127 #endif 128 129 #else /* !CONFIG_JUMP_LABEL */ 130 131 #define _DPRINTK_KEY_INIT 132 133 #ifdef DEBUG 134 #define DYNAMIC_DEBUG_BRANCH(descriptor) \ 135 likely(descriptor.flags & _DPRINTK_FLAGS_PRINT) 136 #else 137 #define DYNAMIC_DEBUG_BRANCH(descriptor) \ 138 unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT) 139 #endif 140 141 #endif /* CONFIG_JUMP_LABEL */ 142 143 /* 144 * Factory macros: ($prefix)dynamic_func_call($suffix) 145 * 146 * Lower layer (with __ prefix) gets the callsite metadata, and wraps 147 * the func inside a debug-branch/static-key construct. Upper layer 148 * (with _ prefix) does the UNIQUE_ID once, so that lower can ref the 149 * name/label multiple times, and tie the elements together. 150 * Multiple flavors: 151 * (|_cls): adds in _DPRINT_CLASS_DFLT as needed 152 * (|_no_desc): former gets callsite descriptor as 1st arg (for prdbgs) 153 */ 154 #define __dynamic_func_call_cls(id, cls, fmt, func, ...) do { \ 155 DEFINE_DYNAMIC_DEBUG_METADATA_CLS(id, cls, fmt); \ 156 if (DYNAMIC_DEBUG_BRANCH(id)) \ 157 func(&id, ##__VA_ARGS__); \ 158 } while (0) 159 #define __dynamic_func_call(id, fmt, func, ...) \ 160 __dynamic_func_call_cls(id, _DPRINTK_CLASS_DFLT, fmt, \ 161 func, ##__VA_ARGS__) 162 163 #define __dynamic_func_call_cls_no_desc(id, cls, fmt, func, ...) do { \ 164 DEFINE_DYNAMIC_DEBUG_METADATA_CLS(id, cls, fmt); \ 165 if (DYNAMIC_DEBUG_BRANCH(id)) \ 166 func(__VA_ARGS__); \ 167 } while (0) 168 #define __dynamic_func_call_no_desc(id, fmt, func, ...) \ 169 __dynamic_func_call_cls_no_desc(id, _DPRINTK_CLASS_DFLT, \ 170 fmt, func, ##__VA_ARGS__) 171 172 /* 173 * "Factory macro" for generating a call to func, guarded by a 174 * DYNAMIC_DEBUG_BRANCH. The dynamic debug descriptor will be 175 * initialized using the fmt argument. The function will be called with 176 * the address of the descriptor as first argument, followed by all 177 * the varargs. Note that fmt is repeated in invocations of this 178 * macro. 179 */ 180 #define _dynamic_func_call_cls(cls, fmt, func, ...) \ 181 __dynamic_func_call_cls(__UNIQUE_ID(ddebug), cls, fmt, func, ##__VA_ARGS__) 182 #define _dynamic_func_call(fmt, func, ...) \ 183 _dynamic_func_call_cls(_DPRINTK_CLASS_DFLT, fmt, func, ##__VA_ARGS__) 184 185 /* 186 * A variant that does the same, except that the descriptor is not 187 * passed as the first argument to the function; it is only called 188 * with precisely the macro's varargs. 189 */ 190 #define _dynamic_func_call_cls_no_desc(cls, fmt, func, ...) \ 191 __dynamic_func_call_cls_no_desc(__UNIQUE_ID(ddebug), cls, fmt, \ 192 func, ##__VA_ARGS__) 193 #define _dynamic_func_call_no_desc(fmt, func, ...) \ 194 _dynamic_func_call_cls_no_desc(_DPRINTK_CLASS_DFLT, fmt, \ 195 func, ##__VA_ARGS__) 196 197 #define dynamic_pr_debug_cls(cls, fmt, ...) \ 198 _dynamic_func_call_cls(cls, fmt, __dynamic_pr_debug, \ 199 pr_fmt(fmt), ##__VA_ARGS__) 200 201 #define dynamic_pr_debug(fmt, ...) \ 202 _dynamic_func_call(fmt, __dynamic_pr_debug, \ 203 pr_fmt(fmt), ##__VA_ARGS__) 204 205 #define dynamic_dev_dbg(dev, fmt, ...) \ 206 _dynamic_func_call(fmt, __dynamic_dev_dbg, \ 207 dev, fmt, ##__VA_ARGS__) 208 209 #define dynamic_netdev_dbg(dev, fmt, ...) \ 210 _dynamic_func_call(fmt, __dynamic_netdev_dbg, \ 211 dev, fmt, ##__VA_ARGS__) 212 213 #define dynamic_ibdev_dbg(dev, fmt, ...) \ 214 _dynamic_func_call(fmt, __dynamic_ibdev_dbg, \ 215 dev, fmt, ##__VA_ARGS__) 216 217 #define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \ 218 groupsize, buf, len, ascii) \ 219 _dynamic_func_call_no_desc(__builtin_constant_p(prefix_str) ? prefix_str : "hexdump", \ 220 print_hex_dump, \ 221 KERN_DEBUG, prefix_str, prefix_type, \ 222 rowsize, groupsize, buf, len, ascii) 223 224 #else /* !CONFIG_DYNAMIC_DEBUG_CORE */ 225 226 #include <linux/string.h> 227 #include <linux/errno.h> 228 #include <linux/printk.h> 229 230 static inline int ddebug_add_module(struct _ddebug_info *dinfo, const char *modname) 231 { 232 return 0; 233 } 234 235 static inline int ddebug_remove_module(const char *mod) 236 { 237 return 0; 238 } 239 240 static inline int ddebug_dyndbg_module_param_cb(char *param, char *val, 241 const char *modname) 242 { 243 if (!strcmp(param, "dyndbg")) { 244 /* avoid pr_warn(), which wants pr_fmt() fully defined */ 245 printk(KERN_WARNING "dyndbg param is supported only in " 246 "CONFIG_DYNAMIC_DEBUG builds\n"); 247 return 0; /* allow and ignore */ 248 } 249 return -EINVAL; 250 } 251 252 #define dynamic_pr_debug(fmt, ...) \ 253 do { if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); } while (0) 254 #define dynamic_dev_dbg(dev, fmt, ...) \ 255 do { if (0) dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); } while (0) 256 #define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \ 257 groupsize, buf, len, ascii) \ 258 do { if (0) \ 259 print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, \ 260 rowsize, groupsize, buf, len, ascii); \ 261 } while (0) 262 263 #endif /* !CONFIG_DYNAMIC_DEBUG_CORE */ 264 265 #endif 266