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) 35*640d1eafSJim Cromie 36*640d1eafSJim Cromie #define _DPRINTK_FLAGS_INCL_ANY \ 37*640d1eafSJim Cromie (_DPRINTK_FLAGS_INCL_MODNAME | _DPRINTK_FLAGS_INCL_FUNCNAME |\ 38*640d1eafSJim Cromie _DPRINTK_FLAGS_INCL_LINENO | _DPRINTK_FLAGS_INCL_TID) 39*640d1eafSJim Cromie 40b558c96fSJim Cromie #if defined DEBUG 41b558c96fSJim Cromie #define _DPRINTK_FLAGS_DEFAULT _DPRINTK_FLAGS_PRINT 42b558c96fSJim Cromie #else 43e9d376f0SJason Baron #define _DPRINTK_FLAGS_DEFAULT 0 44b558c96fSJim Cromie #endif 45e9d376f0SJason Baron unsigned int flags:8; 46e9666d10SMasahiro Yamada #ifdef CONFIG_JUMP_LABEL 479049fc74SJason Baron union { 489049fc74SJason Baron struct static_key_true dd_key_true; 499049fc74SJason Baron struct static_key_false dd_key_false; 509049fc74SJason Baron } key; 519049fc74SJason Baron #endif 52e9d376f0SJason Baron } __attribute__((aligned(8))); 53e9d376f0SJason Baron 54e9d376f0SJason Baron 55e9d376f0SJason Baron 56ceabef7dSOrson Zhai #if defined(CONFIG_DYNAMIC_DEBUG_CORE) 57a2d375edSJim Cromie 58a2d375edSJim Cromie /* exported for module authors to exercise >control */ 59a2d375edSJim Cromie int dynamic_debug_exec_queries(const char *query, const char *modname); 60a2d375edSJim Cromie 61a4507fedSRasmus Villemoes int ddebug_add_module(struct _ddebug *tab, unsigned int n, 62a4507fedSRasmus Villemoes const char *modname); 63ff49d74aSYehuda Sadeh extern int ddebug_remove_module(const char *mod_name); 64b9075fa9SJoe Perches extern __printf(2, 3) 65906d2015SJoe Perches void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...); 66e9d376f0SJason Baron 67b48420c1SJim Cromie extern int ddebug_dyndbg_module_param_cb(char *param, char *val, 68b48420c1SJim Cromie const char *modname); 69b48420c1SJim Cromie 70cbc46635SJoe Perches struct device; 71cbc46635SJoe Perches 72b9075fa9SJoe Perches extern __printf(3, 4) 73906d2015SJoe Perches void __dynamic_dev_dbg(struct _ddebug *descriptor, const struct device *dev, 74b9075fa9SJoe Perches const char *fmt, ...); 75cbc46635SJoe Perches 76ffa10cb4SJason Baron struct net_device; 77ffa10cb4SJason Baron 78b9075fa9SJoe Perches extern __printf(3, 4) 79906d2015SJoe Perches void __dynamic_netdev_dbg(struct _ddebug *descriptor, 80ffa10cb4SJason Baron const struct net_device *dev, 81b9075fa9SJoe Perches const char *fmt, ...); 82ffa10cb4SJason Baron 83923abb9dSGal Pressman struct ib_device; 84923abb9dSGal Pressman 85923abb9dSGal Pressman extern __printf(3, 4) 86923abb9dSGal Pressman void __dynamic_ibdev_dbg(struct _ddebug *descriptor, 87923abb9dSGal Pressman const struct ib_device *ibdev, 88923abb9dSGal Pressman const char *fmt, ...); 89923abb9dSGal Pressman 902bdde670SRasmus Villemoes #define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \ 91c0d2af63SJoe Perches static struct _ddebug __aligned(8) \ 9233def849SJoe Perches __section("__dyndbg") name = { \ 9307613b0bSJason Baron .modname = KBUILD_MODNAME, \ 9407613b0bSJason Baron .function = __func__, \ 9507613b0bSJason Baron .filename = __FILE__, \ 9607613b0bSJason Baron .format = (fmt), \ 9707613b0bSJason Baron .lineno = __LINE__, \ 9807613b0bSJason Baron .flags = _DPRINTK_FLAGS_DEFAULT, \ 992bdde670SRasmus Villemoes _DPRINTK_KEY_INIT \ 10007613b0bSJason Baron } 10107613b0bSJason Baron 102e9666d10SMasahiro Yamada #ifdef CONFIG_JUMP_LABEL 1039049fc74SJason Baron 1049049fc74SJason Baron #ifdef DEBUG 1052bdde670SRasmus Villemoes 1062bdde670SRasmus Villemoes #define _DPRINTK_KEY_INIT .key.dd_key_true = (STATIC_KEY_TRUE_INIT) 1079049fc74SJason Baron 1089049fc74SJason Baron #define DYNAMIC_DEBUG_BRANCH(descriptor) \ 1099049fc74SJason Baron static_branch_likely(&descriptor.key.dd_key_true) 1109049fc74SJason Baron #else 1112bdde670SRasmus Villemoes #define _DPRINTK_KEY_INIT .key.dd_key_false = (STATIC_KEY_FALSE_INIT) 1129049fc74SJason Baron 1139049fc74SJason Baron #define DYNAMIC_DEBUG_BRANCH(descriptor) \ 1149049fc74SJason Baron static_branch_unlikely(&descriptor.key.dd_key_false) 1159049fc74SJason Baron #endif 1169049fc74SJason Baron 117a2d375edSJim Cromie #else /* !CONFIG_JUMP_LABEL */ 1189049fc74SJason Baron 1192bdde670SRasmus Villemoes #define _DPRINTK_KEY_INIT 1209049fc74SJason Baron 1219049fc74SJason Baron #ifdef DEBUG 1229049fc74SJason Baron #define DYNAMIC_DEBUG_BRANCH(descriptor) \ 1239049fc74SJason Baron likely(descriptor.flags & _DPRINTK_FLAGS_PRINT) 1249049fc74SJason Baron #else 1259049fc74SJason Baron #define DYNAMIC_DEBUG_BRANCH(descriptor) \ 1269049fc74SJason Baron unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT) 1279049fc74SJason Baron #endif 1289049fc74SJason Baron 129a2d375edSJim Cromie #endif /* CONFIG_JUMP_LABEL */ 1309049fc74SJason Baron 13147cdd64bSRasmus Villemoes #define __dynamic_func_call(id, fmt, func, ...) do { \ 13247cdd64bSRasmus Villemoes DEFINE_DYNAMIC_DEBUG_METADATA(id, fmt); \ 13347cdd64bSRasmus Villemoes if (DYNAMIC_DEBUG_BRANCH(id)) \ 13447cdd64bSRasmus Villemoes func(&id, ##__VA_ARGS__); \ 135e9d376f0SJason Baron } while (0) 136e9d376f0SJason Baron 13747cdd64bSRasmus Villemoes #define __dynamic_func_call_no_desc(id, fmt, func, ...) do { \ 13847cdd64bSRasmus Villemoes DEFINE_DYNAMIC_DEBUG_METADATA(id, fmt); \ 13947cdd64bSRasmus Villemoes if (DYNAMIC_DEBUG_BRANCH(id)) \ 14047cdd64bSRasmus Villemoes func(__VA_ARGS__); \ 14147cdd64bSRasmus Villemoes } while (0) 14247cdd64bSRasmus Villemoes 14347cdd64bSRasmus Villemoes /* 14447cdd64bSRasmus Villemoes * "Factory macro" for generating a call to func, guarded by a 145e5ebffe1SJim Cromie * DYNAMIC_DEBUG_BRANCH. The dynamic debug descriptor will be 14647cdd64bSRasmus Villemoes * initialized using the fmt argument. The function will be called with 14747cdd64bSRasmus Villemoes * the address of the descriptor as first argument, followed by all 14847cdd64bSRasmus Villemoes * the varargs. Note that fmt is repeated in invocations of this 14947cdd64bSRasmus Villemoes * macro. 15047cdd64bSRasmus Villemoes */ 15147cdd64bSRasmus Villemoes #define _dynamic_func_call(fmt, func, ...) \ 15247cdd64bSRasmus Villemoes __dynamic_func_call(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__) 15347cdd64bSRasmus Villemoes /* 15447cdd64bSRasmus Villemoes * A variant that does the same, except that the descriptor is not 15547cdd64bSRasmus Villemoes * passed as the first argument to the function; it is only called 15647cdd64bSRasmus Villemoes * with precisely the macro's varargs. 15747cdd64bSRasmus Villemoes */ 15847cdd64bSRasmus Villemoes #define _dynamic_func_call_no_desc(fmt, func, ...) \ 15947cdd64bSRasmus Villemoes __dynamic_func_call_no_desc(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__) 16047cdd64bSRasmus Villemoes 16147cdd64bSRasmus Villemoes #define dynamic_pr_debug(fmt, ...) \ 16247cdd64bSRasmus Villemoes _dynamic_func_call(fmt, __dynamic_pr_debug, \ 16347cdd64bSRasmus Villemoes pr_fmt(fmt), ##__VA_ARGS__) 16447cdd64bSRasmus Villemoes 16507613b0bSJason Baron #define dynamic_dev_dbg(dev, fmt, ...) \ 16647cdd64bSRasmus Villemoes _dynamic_func_call(fmt,__dynamic_dev_dbg, \ 16747cdd64bSRasmus Villemoes dev, fmt, ##__VA_ARGS__) 168e9d376f0SJason Baron 16907613b0bSJason Baron #define dynamic_netdev_dbg(dev, fmt, ...) \ 17047cdd64bSRasmus Villemoes _dynamic_func_call(fmt, __dynamic_netdev_dbg, \ 17147cdd64bSRasmus Villemoes dev, fmt, ##__VA_ARGS__) 172ffa10cb4SJason Baron 173923abb9dSGal Pressman #define dynamic_ibdev_dbg(dev, fmt, ...) \ 174923abb9dSGal Pressman _dynamic_func_call(fmt, __dynamic_ibdev_dbg, \ 175923abb9dSGal Pressman dev, fmt, ##__VA_ARGS__) 176923abb9dSGal Pressman 1777a555613SVladimir Kondratiev #define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \ 1787a555613SVladimir Kondratiev groupsize, buf, len, ascii) \ 17947cdd64bSRasmus Villemoes _dynamic_func_call_no_desc(__builtin_constant_p(prefix_str) ? prefix_str : "hexdump", \ 18047cdd64bSRasmus Villemoes print_hex_dump, \ 18147cdd64bSRasmus Villemoes KERN_DEBUG, prefix_str, prefix_type, \ 18247cdd64bSRasmus Villemoes rowsize, groupsize, buf, len, ascii) 1837a555613SVladimir Kondratiev 184a2d375edSJim Cromie #else /* !CONFIG_DYNAMIC_DEBUG_CORE */ 185e9d376f0SJason Baron 186b48420c1SJim Cromie #include <linux/string.h> 187b48420c1SJim Cromie #include <linux/errno.h> 188a2d375edSJim Cromie #include <linux/printk.h> 189b48420c1SJim Cromie 190a4507fedSRasmus Villemoes static inline int ddebug_add_module(struct _ddebug *tab, unsigned int n, 191a4507fedSRasmus Villemoes const char *modname) 192a4507fedSRasmus Villemoes { 193a4507fedSRasmus Villemoes return 0; 194a4507fedSRasmus Villemoes } 195a4507fedSRasmus Villemoes 196ff49d74aSYehuda Sadeh static inline int ddebug_remove_module(const char *mod) 197e9d376f0SJason Baron { 198e9d376f0SJason Baron return 0; 199e9d376f0SJason Baron } 200e9d376f0SJason Baron 201b48420c1SJim Cromie static inline int ddebug_dyndbg_module_param_cb(char *param, char *val, 202b48420c1SJim Cromie const char *modname) 203b48420c1SJim Cromie { 204b48420c1SJim Cromie if (strstr(param, "dyndbg")) { 205516cf1beSJim Cromie /* avoid pr_warn(), which wants pr_fmt() fully defined */ 206516cf1beSJim Cromie printk(KERN_WARNING "dyndbg param is supported only in " 207b48420c1SJim Cromie "CONFIG_DYNAMIC_DEBUG builds\n"); 208b48420c1SJim Cromie return 0; /* allow and ignore */ 209b48420c1SJim Cromie } 210b48420c1SJim Cromie return -EINVAL; 211b48420c1SJim Cromie } 212b48420c1SJim Cromie 21300b55864SJoe Perches #define dynamic_pr_debug(fmt, ...) \ 21400b55864SJoe Perches do { if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); } while (0) 215be70e267SPhilipp Reisner #define dynamic_dev_dbg(dev, fmt, ...) \ 21600b55864SJoe Perches do { if (0) dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); } while (0) 217011c7289SArnd Bergmann #define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \ 218011c7289SArnd Bergmann groupsize, buf, len, ascii) \ 219011c7289SArnd Bergmann do { if (0) \ 220011c7289SArnd Bergmann print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, \ 221011c7289SArnd Bergmann rowsize, groupsize, buf, len, ascii); \ 222011c7289SArnd Bergmann } while (0) 223a2d375edSJim Cromie 224a2d375edSJim Cromie static inline int dynamic_debug_exec_queries(const char *query, const char *modname) 225a2d375edSJim Cromie { 226a2d375edSJim Cromie pr_warn("kernel not built with CONFIG_DYNAMIC_DEBUG_CORE\n"); 227a2d375edSJim Cromie return 0; 228a2d375edSJim Cromie } 229a2d375edSJim Cromie 230a2d375edSJim Cromie #endif /* !CONFIG_DYNAMIC_DEBUG_CORE */ 231e9d376f0SJason Baron 232e9d376f0SJason Baron #endif 233