1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 2e9d376f0SJason Baron #ifndef _DYNAMIC_DEBUG_H 3e9d376f0SJason Baron #define _DYNAMIC_DEBUG_H 4e9d376f0SJason Baron 5e9666d10SMasahiro Yamada #if defined(CONFIG_JUMP_LABEL) 69049fc74SJason Baron #include <linux/jump_label.h> 79049fc74SJason Baron #endif 89049fc74SJason Baron 9e9d376f0SJason Baron /* 10e9d376f0SJason Baron * An instance of this structure is created in a special 11e9d376f0SJason Baron * ELF section at every dynamic debug callsite. At runtime, 12e9d376f0SJason Baron * the special section is treated as an array of these. 13e9d376f0SJason Baron */ 14e9d376f0SJason Baron struct _ddebug { 15e9d376f0SJason Baron /* 16e9d376f0SJason Baron * These fields are used to drive the user interface 17e9d376f0SJason Baron * for selecting and displaying debug callsites. 18e9d376f0SJason Baron */ 19e9d376f0SJason Baron const char *modname; 20e9d376f0SJason Baron const char *function; 21e9d376f0SJason Baron const char *filename; 22e9d376f0SJason Baron const char *format; 23e703ddaeSJim Cromie unsigned int lineno:18; 24e9d376f0SJason Baron /* 25e9d376f0SJason Baron * The flags field controls the behaviour at the callsite. 26e9d376f0SJason Baron * The bits here are changed dynamically when the user 272b2f68b5SFlorian Ragwitz * writes commands to <debugfs>/dynamic_debug/control 28e9d376f0SJason Baron */ 295ca7d2a6SJim Cromie #define _DPRINTK_FLAGS_NONE 0 30e9d376f0SJason Baron #define _DPRINTK_FLAGS_PRINT (1<<0) /* printk() a message using the format */ 318ba6ebf5SBart Van Assche #define _DPRINTK_FLAGS_INCL_MODNAME (1<<1) 328ba6ebf5SBart Van Assche #define _DPRINTK_FLAGS_INCL_FUNCNAME (1<<2) 338ba6ebf5SBart Van Assche #define _DPRINTK_FLAGS_INCL_LINENO (1<<3) 348ba6ebf5SBart Van Assche #define _DPRINTK_FLAGS_INCL_TID (1<<4) 35b558c96fSJim Cromie #if defined DEBUG 36b558c96fSJim Cromie #define _DPRINTK_FLAGS_DEFAULT _DPRINTK_FLAGS_PRINT 37b558c96fSJim Cromie #else 38e9d376f0SJason Baron #define _DPRINTK_FLAGS_DEFAULT 0 39b558c96fSJim Cromie #endif 40e9d376f0SJason Baron unsigned int flags:8; 41e9666d10SMasahiro Yamada #ifdef CONFIG_JUMP_LABEL 429049fc74SJason Baron union { 439049fc74SJason Baron struct static_key_true dd_key_true; 449049fc74SJason Baron struct static_key_false dd_key_false; 459049fc74SJason Baron } key; 469049fc74SJason Baron #endif 47e9d376f0SJason Baron } __attribute__((aligned(8))); 48e9d376f0SJason Baron 49e9d376f0SJason Baron 50e9d376f0SJason Baron 51e9d376f0SJason Baron #if defined(CONFIG_DYNAMIC_DEBUG) 52a4507fedSRasmus Villemoes int ddebug_add_module(struct _ddebug *tab, unsigned int n, 53a4507fedSRasmus Villemoes const char *modname); 54ff49d74aSYehuda Sadeh extern int ddebug_remove_module(const char *mod_name); 55b9075fa9SJoe Perches extern __printf(2, 3) 56906d2015SJoe Perches void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...); 57e9d376f0SJason Baron 58b48420c1SJim Cromie extern int ddebug_dyndbg_module_param_cb(char *param, char *val, 59b48420c1SJim Cromie const char *modname); 60b48420c1SJim Cromie 61cbc46635SJoe Perches struct device; 62cbc46635SJoe Perches 63b9075fa9SJoe Perches extern __printf(3, 4) 64906d2015SJoe Perches void __dynamic_dev_dbg(struct _ddebug *descriptor, const struct device *dev, 65b9075fa9SJoe Perches const char *fmt, ...); 66cbc46635SJoe Perches 67ffa10cb4SJason Baron struct net_device; 68ffa10cb4SJason Baron 69b9075fa9SJoe Perches extern __printf(3, 4) 70906d2015SJoe Perches void __dynamic_netdev_dbg(struct _ddebug *descriptor, 71ffa10cb4SJason Baron const struct net_device *dev, 72b9075fa9SJoe Perches const char *fmt, ...); 73ffa10cb4SJason Baron 742bdde670SRasmus Villemoes #define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \ 75c0d2af63SJoe Perches static struct _ddebug __aligned(8) \ 7607613b0bSJason Baron __attribute__((section("__verbose"))) name = { \ 7707613b0bSJason Baron .modname = KBUILD_MODNAME, \ 7807613b0bSJason Baron .function = __func__, \ 7907613b0bSJason Baron .filename = __FILE__, \ 8007613b0bSJason Baron .format = (fmt), \ 8107613b0bSJason Baron .lineno = __LINE__, \ 8207613b0bSJason Baron .flags = _DPRINTK_FLAGS_DEFAULT, \ 832bdde670SRasmus Villemoes _DPRINTK_KEY_INIT \ 8407613b0bSJason Baron } 8507613b0bSJason Baron 86e9666d10SMasahiro Yamada #ifdef CONFIG_JUMP_LABEL 879049fc74SJason Baron 889049fc74SJason Baron #ifdef DEBUG 892bdde670SRasmus Villemoes 902bdde670SRasmus Villemoes #define _DPRINTK_KEY_INIT .key.dd_key_true = (STATIC_KEY_TRUE_INIT) 919049fc74SJason Baron 929049fc74SJason Baron #define DYNAMIC_DEBUG_BRANCH(descriptor) \ 939049fc74SJason Baron static_branch_likely(&descriptor.key.dd_key_true) 949049fc74SJason Baron #else 952bdde670SRasmus Villemoes #define _DPRINTK_KEY_INIT .key.dd_key_false = (STATIC_KEY_FALSE_INIT) 969049fc74SJason Baron 979049fc74SJason Baron #define DYNAMIC_DEBUG_BRANCH(descriptor) \ 989049fc74SJason Baron static_branch_unlikely(&descriptor.key.dd_key_false) 999049fc74SJason Baron #endif 1009049fc74SJason Baron 1012bdde670SRasmus Villemoes #else /* !HAVE_JUMP_LABEL */ 1029049fc74SJason Baron 1032bdde670SRasmus Villemoes #define _DPRINTK_KEY_INIT 1049049fc74SJason Baron 1059049fc74SJason Baron #ifdef DEBUG 1069049fc74SJason Baron #define DYNAMIC_DEBUG_BRANCH(descriptor) \ 1079049fc74SJason Baron likely(descriptor.flags & _DPRINTK_FLAGS_PRINT) 1089049fc74SJason Baron #else 1099049fc74SJason Baron #define DYNAMIC_DEBUG_BRANCH(descriptor) \ 1109049fc74SJason Baron unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT) 1119049fc74SJason Baron #endif 1129049fc74SJason Baron 1139049fc74SJason Baron #endif 1149049fc74SJason Baron 115*47cdd64bSRasmus Villemoes #define __dynamic_func_call(id, fmt, func, ...) do { \ 116*47cdd64bSRasmus Villemoes DEFINE_DYNAMIC_DEBUG_METADATA(id, fmt); \ 117*47cdd64bSRasmus Villemoes if (DYNAMIC_DEBUG_BRANCH(id)) \ 118*47cdd64bSRasmus Villemoes func(&id, ##__VA_ARGS__); \ 119e9d376f0SJason Baron } while (0) 120e9d376f0SJason Baron 121*47cdd64bSRasmus Villemoes #define __dynamic_func_call_no_desc(id, fmt, func, ...) do { \ 122*47cdd64bSRasmus Villemoes DEFINE_DYNAMIC_DEBUG_METADATA(id, fmt); \ 123*47cdd64bSRasmus Villemoes if (DYNAMIC_DEBUG_BRANCH(id)) \ 124*47cdd64bSRasmus Villemoes func(__VA_ARGS__); \ 125*47cdd64bSRasmus Villemoes } while (0) 126*47cdd64bSRasmus Villemoes 127*47cdd64bSRasmus Villemoes /* 128*47cdd64bSRasmus Villemoes * "Factory macro" for generating a call to func, guarded by a 129*47cdd64bSRasmus Villemoes * DYNAMIC_DEBUG_BRANCH. The dynamic debug decriptor will be 130*47cdd64bSRasmus Villemoes * initialized using the fmt argument. The function will be called with 131*47cdd64bSRasmus Villemoes * the address of the descriptor as first argument, followed by all 132*47cdd64bSRasmus Villemoes * the varargs. Note that fmt is repeated in invocations of this 133*47cdd64bSRasmus Villemoes * macro. 134*47cdd64bSRasmus Villemoes */ 135*47cdd64bSRasmus Villemoes #define _dynamic_func_call(fmt, func, ...) \ 136*47cdd64bSRasmus Villemoes __dynamic_func_call(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__) 137*47cdd64bSRasmus Villemoes /* 138*47cdd64bSRasmus Villemoes * A variant that does the same, except that the descriptor is not 139*47cdd64bSRasmus Villemoes * passed as the first argument to the function; it is only called 140*47cdd64bSRasmus Villemoes * with precisely the macro's varargs. 141*47cdd64bSRasmus Villemoes */ 142*47cdd64bSRasmus Villemoes #define _dynamic_func_call_no_desc(fmt, func, ...) \ 143*47cdd64bSRasmus Villemoes __dynamic_func_call_no_desc(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__) 144*47cdd64bSRasmus Villemoes 145*47cdd64bSRasmus Villemoes #define dynamic_pr_debug(fmt, ...) \ 146*47cdd64bSRasmus Villemoes _dynamic_func_call(fmt, __dynamic_pr_debug, \ 147*47cdd64bSRasmus Villemoes pr_fmt(fmt), ##__VA_ARGS__) 148*47cdd64bSRasmus Villemoes 14907613b0bSJason Baron #define dynamic_dev_dbg(dev, fmt, ...) \ 150*47cdd64bSRasmus Villemoes _dynamic_func_call(fmt,__dynamic_dev_dbg, \ 151*47cdd64bSRasmus Villemoes dev, fmt, ##__VA_ARGS__) 152e9d376f0SJason Baron 15307613b0bSJason Baron #define dynamic_netdev_dbg(dev, fmt, ...) \ 154*47cdd64bSRasmus Villemoes _dynamic_func_call(fmt, __dynamic_netdev_dbg, \ 155*47cdd64bSRasmus Villemoes dev, fmt, ##__VA_ARGS__) 156ffa10cb4SJason Baron 1577a555613SVladimir Kondratiev #define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \ 1587a555613SVladimir Kondratiev groupsize, buf, len, ascii) \ 159*47cdd64bSRasmus Villemoes _dynamic_func_call_no_desc(__builtin_constant_p(prefix_str) ? prefix_str : "hexdump", \ 160*47cdd64bSRasmus Villemoes print_hex_dump, \ 161*47cdd64bSRasmus Villemoes KERN_DEBUG, prefix_str, prefix_type, \ 162*47cdd64bSRasmus Villemoes rowsize, groupsize, buf, len, ascii) 1637a555613SVladimir Kondratiev 164e9d376f0SJason Baron #else 165e9d376f0SJason Baron 166b48420c1SJim Cromie #include <linux/string.h> 167b48420c1SJim Cromie #include <linux/errno.h> 168b48420c1SJim Cromie 169a4507fedSRasmus Villemoes static inline int ddebug_add_module(struct _ddebug *tab, unsigned int n, 170a4507fedSRasmus Villemoes const char *modname) 171a4507fedSRasmus Villemoes { 172a4507fedSRasmus Villemoes return 0; 173a4507fedSRasmus Villemoes } 174a4507fedSRasmus Villemoes 175ff49d74aSYehuda Sadeh static inline int ddebug_remove_module(const char *mod) 176e9d376f0SJason Baron { 177e9d376f0SJason Baron return 0; 178e9d376f0SJason Baron } 179e9d376f0SJason Baron 180b48420c1SJim Cromie static inline int ddebug_dyndbg_module_param_cb(char *param, char *val, 181b48420c1SJim Cromie const char *modname) 182b48420c1SJim Cromie { 183b48420c1SJim Cromie if (strstr(param, "dyndbg")) { 184516cf1beSJim Cromie /* avoid pr_warn(), which wants pr_fmt() fully defined */ 185516cf1beSJim Cromie printk(KERN_WARNING "dyndbg param is supported only in " 186b48420c1SJim Cromie "CONFIG_DYNAMIC_DEBUG builds\n"); 187b48420c1SJim Cromie return 0; /* allow and ignore */ 188b48420c1SJim Cromie } 189b48420c1SJim Cromie return -EINVAL; 190b48420c1SJim Cromie } 191b48420c1SJim Cromie 19200b55864SJoe Perches #define dynamic_pr_debug(fmt, ...) \ 19300b55864SJoe Perches do { if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); } while (0) 194be70e267SPhilipp Reisner #define dynamic_dev_dbg(dev, fmt, ...) \ 19500b55864SJoe Perches do { if (0) dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); } while (0) 196e9d376f0SJason Baron #endif 197e9d376f0SJason Baron 198e9d376f0SJason Baron #endif 199