Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 2 | #ifndef _DYNAMIC_DEBUG_H |
| 3 | #define _DYNAMIC_DEBUG_H |
| 4 | |
Masahiro Yamada | e9666d1 | 2018-12-31 00:14:15 +0900 | [diff] [blame] | 5 | #if defined(CONFIG_JUMP_LABEL) |
Jason Baron | 9049fc7 | 2016-08-03 13:46:39 -0700 | [diff] [blame] | 6 | #include <linux/jump_label.h> |
| 7 | #endif |
| 8 | |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 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; |
Jim Cromie | e703ddae | 2011-12-19 17:12:59 -0500 | [diff] [blame] | 23 | unsigned int lineno:18; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 24 | /* |
Jim Cromie | 3faa286 | 2012-04-27 14:30:33 -0600 | [diff] [blame] | 25 | * The flags field controls the behaviour at the callsite. |
| 26 | * The bits here are changed dynamically when the user |
Florian Ragwitz | 2b2f68b | 2010-05-24 14:33:21 -0700 | [diff] [blame] | 27 | * writes commands to <debugfs>/dynamic_debug/control |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 28 | */ |
Jim Cromie | 5ca7d2a | 2011-12-19 17:12:44 -0500 | [diff] [blame] | 29 | #define _DPRINTK_FLAGS_NONE 0 |
| 30 | #define _DPRINTK_FLAGS_PRINT (1<<0) /* printk() a message using the format */ |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 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) |
Jim Cromie | 640d1ea | 2021-05-04 16:22:34 -0600 | [diff] [blame] | 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 | |
Jim Cromie | b558c96 | 2011-12-19 17:11:18 -0500 | [diff] [blame] | 40 | #if defined DEBUG |
| 41 | #define _DPRINTK_FLAGS_DEFAULT _DPRINTK_FLAGS_PRINT |
| 42 | #else |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 43 | #define _DPRINTK_FLAGS_DEFAULT 0 |
Jim Cromie | b558c96 | 2011-12-19 17:11:18 -0500 | [diff] [blame] | 44 | #endif |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 45 | unsigned int flags:8; |
Masahiro Yamada | e9666d1 | 2018-12-31 00:14:15 +0900 | [diff] [blame] | 46 | #ifdef CONFIG_JUMP_LABEL |
Jason Baron | 9049fc7 | 2016-08-03 13:46:39 -0700 | [diff] [blame] | 47 | union { |
| 48 | struct static_key_true dd_key_true; |
| 49 | struct static_key_false dd_key_false; |
| 50 | } key; |
| 51 | #endif |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 52 | } __attribute__((aligned(8))); |
| 53 | |
| 54 | |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 55 | |
Orson Zhai | ceabef7 | 2020-06-07 21:40:14 -0700 | [diff] [blame] | 56 | #if defined(CONFIG_DYNAMIC_DEBUG_CORE) |
Jim Cromie | a2d375e | 2020-08-31 12:22:09 -0600 | [diff] [blame] | 57 | |
Rasmus Villemoes | a4507fe | 2019-03-07 16:27:52 -0800 | [diff] [blame] | 58 | int ddebug_add_module(struct _ddebug *tab, unsigned int n, |
| 59 | const char *modname); |
Yehuda Sadeh | ff49d74 | 2010-07-03 13:07:35 +1000 | [diff] [blame] | 60 | extern int ddebug_remove_module(const char *mod_name); |
Joe Perches | b9075fa | 2011-10-31 17:11:33 -0700 | [diff] [blame] | 61 | extern __printf(2, 3) |
Joe Perches | 906d201 | 2014-09-24 11:17:56 -0700 | [diff] [blame] | 62 | void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 63 | |
Jim Cromie | b48420c | 2012-04-27 14:30:35 -0600 | [diff] [blame] | 64 | extern int ddebug_dyndbg_module_param_cb(char *param, char *val, |
| 65 | const char *modname); |
| 66 | |
Joe Perches | cbc4663 | 2011-08-11 14:36:21 -0400 | [diff] [blame] | 67 | struct device; |
| 68 | |
Joe Perches | b9075fa | 2011-10-31 17:11:33 -0700 | [diff] [blame] | 69 | extern __printf(3, 4) |
Joe Perches | 906d201 | 2014-09-24 11:17:56 -0700 | [diff] [blame] | 70 | void __dynamic_dev_dbg(struct _ddebug *descriptor, const struct device *dev, |
| 71 | const char *fmt, ...); |
Joe Perches | cbc4663 | 2011-08-11 14:36:21 -0400 | [diff] [blame] | 72 | |
Jason Baron | ffa10cb | 2011-08-11 14:36:48 -0400 | [diff] [blame] | 73 | struct net_device; |
| 74 | |
Joe Perches | b9075fa | 2011-10-31 17:11:33 -0700 | [diff] [blame] | 75 | extern __printf(3, 4) |
Joe Perches | 906d201 | 2014-09-24 11:17:56 -0700 | [diff] [blame] | 76 | void __dynamic_netdev_dbg(struct _ddebug *descriptor, |
| 77 | const struct net_device *dev, |
| 78 | const char *fmt, ...); |
Jason Baron | ffa10cb | 2011-08-11 14:36:48 -0400 | [diff] [blame] | 79 | |
Gal Pressman | 923abb9 | 2019-05-01 13:48:13 +0300 | [diff] [blame] | 80 | struct ib_device; |
| 81 | |
| 82 | extern __printf(3, 4) |
| 83 | void __dynamic_ibdev_dbg(struct _ddebug *descriptor, |
| 84 | const struct ib_device *ibdev, |
| 85 | const char *fmt, ...); |
| 86 | |
Rasmus Villemoes | 2bdde67 | 2019-03-07 16:27:33 -0800 | [diff] [blame] | 87 | #define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \ |
Joe Perches | c0d2af6 | 2012-10-18 12:07:03 -0700 | [diff] [blame] | 88 | static struct _ddebug __aligned(8) \ |
Joe Perches | 33def84 | 2020-10-21 19:36:07 -0700 | [diff] [blame] | 89 | __section("__dyndbg") name = { \ |
Jason Baron | 07613b0 | 2011-10-04 14:13:15 -0700 | [diff] [blame] | 90 | .modname = KBUILD_MODNAME, \ |
| 91 | .function = __func__, \ |
| 92 | .filename = __FILE__, \ |
| 93 | .format = (fmt), \ |
| 94 | .lineno = __LINE__, \ |
Jason Baron | 9049fc7 | 2016-08-03 13:46:39 -0700 | [diff] [blame] | 95 | .flags = _DPRINTK_FLAGS_DEFAULT, \ |
Rasmus Villemoes | 2bdde67 | 2019-03-07 16:27:33 -0800 | [diff] [blame] | 96 | _DPRINTK_KEY_INIT \ |
Jason Baron | 07613b0 | 2011-10-04 14:13:15 -0700 | [diff] [blame] | 97 | } |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 98 | |
Masahiro Yamada | e9666d1 | 2018-12-31 00:14:15 +0900 | [diff] [blame] | 99 | #ifdef CONFIG_JUMP_LABEL |
Jason Baron | 9049fc7 | 2016-08-03 13:46:39 -0700 | [diff] [blame] | 100 | |
Jason Baron | 9049fc7 | 2016-08-03 13:46:39 -0700 | [diff] [blame] | 101 | #ifdef DEBUG |
Rasmus Villemoes | 2bdde67 | 2019-03-07 16:27:33 -0800 | [diff] [blame] | 102 | |
| 103 | #define _DPRINTK_KEY_INIT .key.dd_key_true = (STATIC_KEY_TRUE_INIT) |
Jason Baron | 9049fc7 | 2016-08-03 13:46:39 -0700 | [diff] [blame] | 104 | |
| 105 | #define DYNAMIC_DEBUG_BRANCH(descriptor) \ |
| 106 | static_branch_likely(&descriptor.key.dd_key_true) |
| 107 | #else |
Rasmus Villemoes | 2bdde67 | 2019-03-07 16:27:33 -0800 | [diff] [blame] | 108 | #define _DPRINTK_KEY_INIT .key.dd_key_false = (STATIC_KEY_FALSE_INIT) |
Jason Baron | 9049fc7 | 2016-08-03 13:46:39 -0700 | [diff] [blame] | 109 | |
| 110 | #define DYNAMIC_DEBUG_BRANCH(descriptor) \ |
| 111 | static_branch_unlikely(&descriptor.key.dd_key_false) |
| 112 | #endif |
| 113 | |
Jim Cromie | a2d375e | 2020-08-31 12:22:09 -0600 | [diff] [blame] | 114 | #else /* !CONFIG_JUMP_LABEL */ |
Jason Baron | 9049fc7 | 2016-08-03 13:46:39 -0700 | [diff] [blame] | 115 | |
Rasmus Villemoes | 2bdde67 | 2019-03-07 16:27:33 -0800 | [diff] [blame] | 116 | #define _DPRINTK_KEY_INIT |
Jason Baron | 9049fc7 | 2016-08-03 13:46:39 -0700 | [diff] [blame] | 117 | |
| 118 | #ifdef DEBUG |
| 119 | #define DYNAMIC_DEBUG_BRANCH(descriptor) \ |
| 120 | likely(descriptor.flags & _DPRINTK_FLAGS_PRINT) |
| 121 | #else |
| 122 | #define DYNAMIC_DEBUG_BRANCH(descriptor) \ |
| 123 | unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT) |
| 124 | #endif |
| 125 | |
Jim Cromie | a2d375e | 2020-08-31 12:22:09 -0600 | [diff] [blame] | 126 | #endif /* CONFIG_JUMP_LABEL */ |
Jason Baron | 9049fc7 | 2016-08-03 13:46:39 -0700 | [diff] [blame] | 127 | |
Rasmus Villemoes | 47cdd64 | 2019-03-07 16:27:56 -0800 | [diff] [blame] | 128 | #define __dynamic_func_call(id, fmt, func, ...) do { \ |
| 129 | DEFINE_DYNAMIC_DEBUG_METADATA(id, fmt); \ |
| 130 | if (DYNAMIC_DEBUG_BRANCH(id)) \ |
| 131 | func(&id, ##__VA_ARGS__); \ |
Jason Baron | 07613b0 | 2011-10-04 14:13:15 -0700 | [diff] [blame] | 132 | } while (0) |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 133 | |
Rasmus Villemoes | 47cdd64 | 2019-03-07 16:27:56 -0800 | [diff] [blame] | 134 | #define __dynamic_func_call_no_desc(id, fmt, func, ...) do { \ |
| 135 | DEFINE_DYNAMIC_DEBUG_METADATA(id, fmt); \ |
| 136 | if (DYNAMIC_DEBUG_BRANCH(id)) \ |
| 137 | func(__VA_ARGS__); \ |
| 138 | } while (0) |
| 139 | |
| 140 | /* |
| 141 | * "Factory macro" for generating a call to func, guarded by a |
Jim Cromie | e5ebffe | 2020-07-19 17:10:45 -0600 | [diff] [blame] | 142 | * DYNAMIC_DEBUG_BRANCH. The dynamic debug descriptor will be |
Rasmus Villemoes | 47cdd64 | 2019-03-07 16:27:56 -0800 | [diff] [blame] | 143 | * initialized using the fmt argument. The function will be called with |
| 144 | * the address of the descriptor as first argument, followed by all |
| 145 | * the varargs. Note that fmt is repeated in invocations of this |
| 146 | * macro. |
| 147 | */ |
| 148 | #define _dynamic_func_call(fmt, func, ...) \ |
| 149 | __dynamic_func_call(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__) |
| 150 | /* |
| 151 | * A variant that does the same, except that the descriptor is not |
| 152 | * passed as the first argument to the function; it is only called |
| 153 | * with precisely the macro's varargs. |
| 154 | */ |
| 155 | #define _dynamic_func_call_no_desc(fmt, func, ...) \ |
| 156 | __dynamic_func_call_no_desc(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__) |
| 157 | |
| 158 | #define dynamic_pr_debug(fmt, ...) \ |
| 159 | _dynamic_func_call(fmt, __dynamic_pr_debug, \ |
| 160 | pr_fmt(fmt), ##__VA_ARGS__) |
| 161 | |
Jason Baron | 07613b0 | 2011-10-04 14:13:15 -0700 | [diff] [blame] | 162 | #define dynamic_dev_dbg(dev, fmt, ...) \ |
Rasmus Villemoes | 47cdd64 | 2019-03-07 16:27:56 -0800 | [diff] [blame] | 163 | _dynamic_func_call(fmt,__dynamic_dev_dbg, \ |
| 164 | dev, fmt, ##__VA_ARGS__) |
Jason Baron | 07613b0 | 2011-10-04 14:13:15 -0700 | [diff] [blame] | 165 | |
| 166 | #define dynamic_netdev_dbg(dev, fmt, ...) \ |
Rasmus Villemoes | 47cdd64 | 2019-03-07 16:27:56 -0800 | [diff] [blame] | 167 | _dynamic_func_call(fmt, __dynamic_netdev_dbg, \ |
| 168 | dev, fmt, ##__VA_ARGS__) |
Jason Baron | ffa10cb | 2011-08-11 14:36:48 -0400 | [diff] [blame] | 169 | |
Gal Pressman | 923abb9 | 2019-05-01 13:48:13 +0300 | [diff] [blame] | 170 | #define dynamic_ibdev_dbg(dev, fmt, ...) \ |
| 171 | _dynamic_func_call(fmt, __dynamic_ibdev_dbg, \ |
| 172 | dev, fmt, ##__VA_ARGS__) |
| 173 | |
Rasmus Villemoes | 47cdd64 | 2019-03-07 16:27:56 -0800 | [diff] [blame] | 174 | #define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \ |
| 175 | groupsize, buf, len, ascii) \ |
| 176 | _dynamic_func_call_no_desc(__builtin_constant_p(prefix_str) ? prefix_str : "hexdump", \ |
| 177 | print_hex_dump, \ |
| 178 | KERN_DEBUG, prefix_str, prefix_type, \ |
| 179 | rowsize, groupsize, buf, len, ascii) |
Vladimir Kondratiev | 7a55561 | 2012-12-05 16:48:27 -0500 | [diff] [blame] | 180 | |
Jim Cromie | a2d375e | 2020-08-31 12:22:09 -0600 | [diff] [blame] | 181 | #else /* !CONFIG_DYNAMIC_DEBUG_CORE */ |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 182 | |
Jim Cromie | b48420c | 2012-04-27 14:30:35 -0600 | [diff] [blame] | 183 | #include <linux/string.h> |
| 184 | #include <linux/errno.h> |
Jim Cromie | a2d375e | 2020-08-31 12:22:09 -0600 | [diff] [blame] | 185 | #include <linux/printk.h> |
Jim Cromie | b48420c | 2012-04-27 14:30:35 -0600 | [diff] [blame] | 186 | |
Rasmus Villemoes | a4507fe | 2019-03-07 16:27:52 -0800 | [diff] [blame] | 187 | static inline int ddebug_add_module(struct _ddebug *tab, unsigned int n, |
| 188 | const char *modname) |
| 189 | { |
| 190 | return 0; |
| 191 | } |
| 192 | |
Yehuda Sadeh | ff49d74 | 2010-07-03 13:07:35 +1000 | [diff] [blame] | 193 | static inline int ddebug_remove_module(const char *mod) |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 194 | { |
| 195 | return 0; |
| 196 | } |
| 197 | |
Jim Cromie | b48420c | 2012-04-27 14:30:35 -0600 | [diff] [blame] | 198 | static inline int ddebug_dyndbg_module_param_cb(char *param, char *val, |
| 199 | const char *modname) |
| 200 | { |
Jim Cromie | 0c0d9f3 | 2022-09-04 15:40:39 -0600 | [diff] [blame] | 201 | if (!strcmp(param, "dyndbg")) { |
Jim Cromie | 516cf1b | 2012-05-01 05:23:12 -0600 | [diff] [blame] | 202 | /* avoid pr_warn(), which wants pr_fmt() fully defined */ |
| 203 | printk(KERN_WARNING "dyndbg param is supported only in " |
Jim Cromie | b48420c | 2012-04-27 14:30:35 -0600 | [diff] [blame] | 204 | "CONFIG_DYNAMIC_DEBUG builds\n"); |
| 205 | return 0; /* allow and ignore */ |
| 206 | } |
| 207 | return -EINVAL; |
| 208 | } |
| 209 | |
Joe Perches | 00b55864 | 2009-12-14 18:00:14 -0800 | [diff] [blame] | 210 | #define dynamic_pr_debug(fmt, ...) \ |
| 211 | do { if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); } while (0) |
Philipp Reisner | be70e26 | 2010-10-14 11:58:20 +0200 | [diff] [blame] | 212 | #define dynamic_dev_dbg(dev, fmt, ...) \ |
Joe Perches | 00b55864 | 2009-12-14 18:00:14 -0800 | [diff] [blame] | 213 | do { if (0) dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); } while (0) |
Arnd Bergmann | 011c728 | 2019-09-18 21:55:11 +0200 | [diff] [blame] | 214 | #define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \ |
| 215 | groupsize, buf, len, ascii) \ |
| 216 | do { if (0) \ |
| 217 | print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, \ |
| 218 | rowsize, groupsize, buf, len, ascii); \ |
| 219 | } while (0) |
Jim Cromie | a2d375e | 2020-08-31 12:22:09 -0600 | [diff] [blame] | 220 | |
Jim Cromie | a2d375e | 2020-08-31 12:22:09 -0600 | [diff] [blame] | 221 | #endif /* !CONFIG_DYNAMIC_DEBUG_CORE */ |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 222 | |
| 223 | #endif |