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 51ceabef7dSOrson Zhai #if defined(CONFIG_DYNAMIC_DEBUG_CORE) 52*a2d375edSJim Cromie 53*a2d375edSJim Cromie /* exported for module authors to exercise >control */ 54*a2d375edSJim Cromie int dynamic_debug_exec_queries(const char *query, const char *modname); 55*a2d375edSJim Cromie 56a4507fedSRasmus Villemoes int ddebug_add_module(struct _ddebug *tab, unsigned int n, 57a4507fedSRasmus Villemoes const char *modname); 58ff49d74aSYehuda Sadeh extern int ddebug_remove_module(const char *mod_name); 59b9075fa9SJoe Perches extern __printf(2, 3) 60906d2015SJoe Perches void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...); 61e9d376f0SJason Baron 62b48420c1SJim Cromie extern int ddebug_dyndbg_module_param_cb(char *param, char *val, 63b48420c1SJim Cromie const char *modname); 64b48420c1SJim Cromie 65cbc46635SJoe Perches struct device; 66cbc46635SJoe Perches 67b9075fa9SJoe Perches extern __printf(3, 4) 68906d2015SJoe Perches void __dynamic_dev_dbg(struct _ddebug *descriptor, const struct device *dev, 69b9075fa9SJoe Perches const char *fmt, ...); 70cbc46635SJoe Perches 71ffa10cb4SJason Baron struct net_device; 72ffa10cb4SJason Baron 73b9075fa9SJoe Perches extern __printf(3, 4) 74906d2015SJoe Perches void __dynamic_netdev_dbg(struct _ddebug *descriptor, 75ffa10cb4SJason Baron const struct net_device *dev, 76b9075fa9SJoe Perches const char *fmt, ...); 77ffa10cb4SJason Baron 78923abb9dSGal Pressman struct ib_device; 79923abb9dSGal Pressman 80923abb9dSGal Pressman extern __printf(3, 4) 81923abb9dSGal Pressman void __dynamic_ibdev_dbg(struct _ddebug *descriptor, 82923abb9dSGal Pressman const struct ib_device *ibdev, 83923abb9dSGal Pressman const char *fmt, ...); 84923abb9dSGal Pressman 852bdde670SRasmus Villemoes #define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \ 86c0d2af63SJoe Perches static struct _ddebug __aligned(8) \ 87e5ebffe1SJim Cromie __section(__dyndbg) name = { \ 8807613b0bSJason Baron .modname = KBUILD_MODNAME, \ 8907613b0bSJason Baron .function = __func__, \ 9007613b0bSJason Baron .filename = __FILE__, \ 9107613b0bSJason Baron .format = (fmt), \ 9207613b0bSJason Baron .lineno = __LINE__, \ 9307613b0bSJason Baron .flags = _DPRINTK_FLAGS_DEFAULT, \ 942bdde670SRasmus Villemoes _DPRINTK_KEY_INIT \ 9507613b0bSJason Baron } 9607613b0bSJason Baron 97e9666d10SMasahiro Yamada #ifdef CONFIG_JUMP_LABEL 989049fc74SJason Baron 999049fc74SJason Baron #ifdef DEBUG 1002bdde670SRasmus Villemoes 1012bdde670SRasmus Villemoes #define _DPRINTK_KEY_INIT .key.dd_key_true = (STATIC_KEY_TRUE_INIT) 1029049fc74SJason Baron 1039049fc74SJason Baron #define DYNAMIC_DEBUG_BRANCH(descriptor) \ 1049049fc74SJason Baron static_branch_likely(&descriptor.key.dd_key_true) 1059049fc74SJason Baron #else 1062bdde670SRasmus Villemoes #define _DPRINTK_KEY_INIT .key.dd_key_false = (STATIC_KEY_FALSE_INIT) 1079049fc74SJason Baron 1089049fc74SJason Baron #define DYNAMIC_DEBUG_BRANCH(descriptor) \ 1099049fc74SJason Baron static_branch_unlikely(&descriptor.key.dd_key_false) 1109049fc74SJason Baron #endif 1119049fc74SJason Baron 112*a2d375edSJim Cromie #else /* !CONFIG_JUMP_LABEL */ 1139049fc74SJason Baron 1142bdde670SRasmus Villemoes #define _DPRINTK_KEY_INIT 1159049fc74SJason Baron 1169049fc74SJason Baron #ifdef DEBUG 1179049fc74SJason Baron #define DYNAMIC_DEBUG_BRANCH(descriptor) \ 1189049fc74SJason Baron likely(descriptor.flags & _DPRINTK_FLAGS_PRINT) 1199049fc74SJason Baron #else 1209049fc74SJason Baron #define DYNAMIC_DEBUG_BRANCH(descriptor) \ 1219049fc74SJason Baron unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT) 1229049fc74SJason Baron #endif 1239049fc74SJason Baron 124*a2d375edSJim Cromie #endif /* CONFIG_JUMP_LABEL */ 1259049fc74SJason Baron 12647cdd64bSRasmus Villemoes #define __dynamic_func_call(id, fmt, func, ...) do { \ 12747cdd64bSRasmus Villemoes DEFINE_DYNAMIC_DEBUG_METADATA(id, fmt); \ 12847cdd64bSRasmus Villemoes if (DYNAMIC_DEBUG_BRANCH(id)) \ 12947cdd64bSRasmus Villemoes func(&id, ##__VA_ARGS__); \ 130e9d376f0SJason Baron } while (0) 131e9d376f0SJason Baron 13247cdd64bSRasmus Villemoes #define __dynamic_func_call_no_desc(id, fmt, func, ...) do { \ 13347cdd64bSRasmus Villemoes DEFINE_DYNAMIC_DEBUG_METADATA(id, fmt); \ 13447cdd64bSRasmus Villemoes if (DYNAMIC_DEBUG_BRANCH(id)) \ 13547cdd64bSRasmus Villemoes func(__VA_ARGS__); \ 13647cdd64bSRasmus Villemoes } while (0) 13747cdd64bSRasmus Villemoes 13847cdd64bSRasmus Villemoes /* 13947cdd64bSRasmus Villemoes * "Factory macro" for generating a call to func, guarded by a 140e5ebffe1SJim Cromie * DYNAMIC_DEBUG_BRANCH. The dynamic debug descriptor will be 14147cdd64bSRasmus Villemoes * initialized using the fmt argument. The function will be called with 14247cdd64bSRasmus Villemoes * the address of the descriptor as first argument, followed by all 14347cdd64bSRasmus Villemoes * the varargs. Note that fmt is repeated in invocations of this 14447cdd64bSRasmus Villemoes * macro. 14547cdd64bSRasmus Villemoes */ 14647cdd64bSRasmus Villemoes #define _dynamic_func_call(fmt, func, ...) \ 14747cdd64bSRasmus Villemoes __dynamic_func_call(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__) 14847cdd64bSRasmus Villemoes /* 14947cdd64bSRasmus Villemoes * A variant that does the same, except that the descriptor is not 15047cdd64bSRasmus Villemoes * passed as the first argument to the function; it is only called 15147cdd64bSRasmus Villemoes * with precisely the macro's varargs. 15247cdd64bSRasmus Villemoes */ 15347cdd64bSRasmus Villemoes #define _dynamic_func_call_no_desc(fmt, func, ...) \ 15447cdd64bSRasmus Villemoes __dynamic_func_call_no_desc(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__) 15547cdd64bSRasmus Villemoes 15647cdd64bSRasmus Villemoes #define dynamic_pr_debug(fmt, ...) \ 15747cdd64bSRasmus Villemoes _dynamic_func_call(fmt, __dynamic_pr_debug, \ 15847cdd64bSRasmus Villemoes pr_fmt(fmt), ##__VA_ARGS__) 15947cdd64bSRasmus Villemoes 16007613b0bSJason Baron #define dynamic_dev_dbg(dev, fmt, ...) \ 16147cdd64bSRasmus Villemoes _dynamic_func_call(fmt,__dynamic_dev_dbg, \ 16247cdd64bSRasmus Villemoes dev, fmt, ##__VA_ARGS__) 163e9d376f0SJason Baron 16407613b0bSJason Baron #define dynamic_netdev_dbg(dev, fmt, ...) \ 16547cdd64bSRasmus Villemoes _dynamic_func_call(fmt, __dynamic_netdev_dbg, \ 16647cdd64bSRasmus Villemoes dev, fmt, ##__VA_ARGS__) 167ffa10cb4SJason Baron 168923abb9dSGal Pressman #define dynamic_ibdev_dbg(dev, fmt, ...) \ 169923abb9dSGal Pressman _dynamic_func_call(fmt, __dynamic_ibdev_dbg, \ 170923abb9dSGal Pressman dev, fmt, ##__VA_ARGS__) 171923abb9dSGal Pressman 1727a555613SVladimir Kondratiev #define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \ 1737a555613SVladimir Kondratiev groupsize, buf, len, ascii) \ 17447cdd64bSRasmus Villemoes _dynamic_func_call_no_desc(__builtin_constant_p(prefix_str) ? prefix_str : "hexdump", \ 17547cdd64bSRasmus Villemoes print_hex_dump, \ 17647cdd64bSRasmus Villemoes KERN_DEBUG, prefix_str, prefix_type, \ 17747cdd64bSRasmus Villemoes rowsize, groupsize, buf, len, ascii) 1787a555613SVladimir Kondratiev 179*a2d375edSJim Cromie #else /* !CONFIG_DYNAMIC_DEBUG_CORE */ 180e9d376f0SJason Baron 181b48420c1SJim Cromie #include <linux/string.h> 182b48420c1SJim Cromie #include <linux/errno.h> 183*a2d375edSJim Cromie #include <linux/printk.h> 184b48420c1SJim Cromie 185a4507fedSRasmus Villemoes static inline int ddebug_add_module(struct _ddebug *tab, unsigned int n, 186a4507fedSRasmus Villemoes const char *modname) 187a4507fedSRasmus Villemoes { 188a4507fedSRasmus Villemoes return 0; 189a4507fedSRasmus Villemoes } 190a4507fedSRasmus Villemoes 191ff49d74aSYehuda Sadeh static inline int ddebug_remove_module(const char *mod) 192e9d376f0SJason Baron { 193e9d376f0SJason Baron return 0; 194e9d376f0SJason Baron } 195e9d376f0SJason Baron 196b48420c1SJim Cromie static inline int ddebug_dyndbg_module_param_cb(char *param, char *val, 197b48420c1SJim Cromie const char *modname) 198b48420c1SJim Cromie { 199b48420c1SJim Cromie if (strstr(param, "dyndbg")) { 200516cf1beSJim Cromie /* avoid pr_warn(), which wants pr_fmt() fully defined */ 201516cf1beSJim Cromie printk(KERN_WARNING "dyndbg param is supported only in " 202b48420c1SJim Cromie "CONFIG_DYNAMIC_DEBUG builds\n"); 203b48420c1SJim Cromie return 0; /* allow and ignore */ 204b48420c1SJim Cromie } 205b48420c1SJim Cromie return -EINVAL; 206b48420c1SJim Cromie } 207b48420c1SJim Cromie 20800b55864SJoe Perches #define dynamic_pr_debug(fmt, ...) \ 20900b55864SJoe Perches do { if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); } while (0) 210be70e267SPhilipp Reisner #define dynamic_dev_dbg(dev, fmt, ...) \ 21100b55864SJoe Perches do { if (0) dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); } while (0) 212011c7289SArnd Bergmann #define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \ 213011c7289SArnd Bergmann groupsize, buf, len, ascii) \ 214011c7289SArnd Bergmann do { if (0) \ 215011c7289SArnd Bergmann print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, \ 216011c7289SArnd Bergmann rowsize, groupsize, buf, len, ascii); \ 217011c7289SArnd Bergmann } while (0) 218*a2d375edSJim Cromie 219*a2d375edSJim Cromie static inline int dynamic_debug_exec_queries(const char *query, const char *modname) 220*a2d375edSJim Cromie { 221*a2d375edSJim Cromie pr_warn("kernel not built with CONFIG_DYNAMIC_DEBUG_CORE\n"); 222*a2d375edSJim Cromie return 0; 223*a2d375edSJim Cromie } 224*a2d375edSJim Cromie 225*a2d375edSJim Cromie #endif /* !CONFIG_DYNAMIC_DEBUG_CORE */ 226e9d376f0SJason Baron 227e9d376f0SJason Baron #endif 228