Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 1 | #ifndef _DYNAMIC_DEBUG_H |
| 2 | #define _DYNAMIC_DEBUG_H |
| 3 | |
Jason Baron | 9049fc7 | 2016-08-03 13:46:39 -0700 | [diff] [blame] | 4 | #if defined(CC_HAVE_ASM_GOTO) && defined(CONFIG_JUMP_LABEL) |
| 5 | #include <linux/jump_label.h> |
| 6 | #endif |
| 7 | |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 8 | /* |
| 9 | * An instance of this structure is created in a special |
| 10 | * ELF section at every dynamic debug callsite. At runtime, |
| 11 | * the special section is treated as an array of these. |
| 12 | */ |
| 13 | struct _ddebug { |
| 14 | /* |
| 15 | * These fields are used to drive the user interface |
| 16 | * for selecting and displaying debug callsites. |
| 17 | */ |
| 18 | const char *modname; |
| 19 | const char *function; |
| 20 | const char *filename; |
| 21 | const char *format; |
Jim Cromie | e703ddae | 2011-12-19 17:12:59 -0500 | [diff] [blame] | 22 | unsigned int lineno:18; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 23 | /* |
Jim Cromie | 3faa286 | 2012-04-27 14:30:33 -0600 | [diff] [blame] | 24 | * The flags field controls the behaviour at the callsite. |
| 25 | * The bits here are changed dynamically when the user |
Florian Ragwitz | 2b2f68b | 2010-05-24 14:33:21 -0700 | [diff] [blame] | 26 | * writes commands to <debugfs>/dynamic_debug/control |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 27 | */ |
Jim Cromie | 5ca7d2a | 2011-12-19 17:12:44 -0500 | [diff] [blame] | 28 | #define _DPRINTK_FLAGS_NONE 0 |
| 29 | #define _DPRINTK_FLAGS_PRINT (1<<0) /* printk() a message using the format */ |
Bart Van Assche | 8ba6ebf | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 30 | #define _DPRINTK_FLAGS_INCL_MODNAME (1<<1) |
| 31 | #define _DPRINTK_FLAGS_INCL_FUNCNAME (1<<2) |
| 32 | #define _DPRINTK_FLAGS_INCL_LINENO (1<<3) |
| 33 | #define _DPRINTK_FLAGS_INCL_TID (1<<4) |
Jim Cromie | b558c96 | 2011-12-19 17:11:18 -0500 | [diff] [blame] | 34 | #if defined DEBUG |
| 35 | #define _DPRINTK_FLAGS_DEFAULT _DPRINTK_FLAGS_PRINT |
| 36 | #else |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 37 | #define _DPRINTK_FLAGS_DEFAULT 0 |
Jim Cromie | b558c96 | 2011-12-19 17:11:18 -0500 | [diff] [blame] | 38 | #endif |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 39 | unsigned int flags:8; |
Jason Baron | 9049fc7 | 2016-08-03 13:46:39 -0700 | [diff] [blame] | 40 | #ifdef HAVE_JUMP_LABEL |
| 41 | union { |
| 42 | struct static_key_true dd_key_true; |
| 43 | struct static_key_false dd_key_false; |
| 44 | } key; |
| 45 | #endif |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 46 | } __attribute__((aligned(8))); |
| 47 | |
| 48 | |
| 49 | int ddebug_add_module(struct _ddebug *tab, unsigned int n, |
| 50 | const char *modname); |
| 51 | |
| 52 | #if defined(CONFIG_DYNAMIC_DEBUG) |
Yehuda Sadeh | ff49d74 | 2010-07-03 13:07:35 +1000 | [diff] [blame] | 53 | extern int ddebug_remove_module(const char *mod_name); |
Joe Perches | b9075fa | 2011-10-31 17:11:33 -0700 | [diff] [blame] | 54 | extern __printf(2, 3) |
Joe Perches | 906d201 | 2014-09-24 11:17:56 -0700 | [diff] [blame] | 55 | void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 56 | |
Jim Cromie | b48420c | 2012-04-27 14:30:35 -0600 | [diff] [blame] | 57 | extern int ddebug_dyndbg_module_param_cb(char *param, char *val, |
| 58 | const char *modname); |
| 59 | |
Joe Perches | cbc4663 | 2011-08-11 14:36:21 -0400 | [diff] [blame] | 60 | struct device; |
| 61 | |
Joe Perches | b9075fa | 2011-10-31 17:11:33 -0700 | [diff] [blame] | 62 | extern __printf(3, 4) |
Joe Perches | 906d201 | 2014-09-24 11:17:56 -0700 | [diff] [blame] | 63 | void __dynamic_dev_dbg(struct _ddebug *descriptor, const struct device *dev, |
| 64 | const char *fmt, ...); |
Joe Perches | cbc4663 | 2011-08-11 14:36:21 -0400 | [diff] [blame] | 65 | |
Jason Baron | ffa10cb | 2011-08-11 14:36:48 -0400 | [diff] [blame] | 66 | struct net_device; |
| 67 | |
Joe Perches | b9075fa | 2011-10-31 17:11:33 -0700 | [diff] [blame] | 68 | extern __printf(3, 4) |
Joe Perches | 906d201 | 2014-09-24 11:17:56 -0700 | [diff] [blame] | 69 | void __dynamic_netdev_dbg(struct _ddebug *descriptor, |
| 70 | const struct net_device *dev, |
| 71 | const char *fmt, ...); |
Jason Baron | ffa10cb | 2011-08-11 14:36:48 -0400 | [diff] [blame] | 72 | |
Jason Baron | 9049fc7 | 2016-08-03 13:46:39 -0700 | [diff] [blame] | 73 | #define DEFINE_DYNAMIC_DEBUG_METADATA_KEY(name, fmt, key, init) \ |
Joe Perches | c0d2af6 | 2012-10-18 12:07:03 -0700 | [diff] [blame] | 74 | static struct _ddebug __aligned(8) \ |
Jason Baron | 07613b0 | 2011-10-04 14:13:15 -0700 | [diff] [blame] | 75 | __attribute__((section("__verbose"))) name = { \ |
| 76 | .modname = KBUILD_MODNAME, \ |
| 77 | .function = __func__, \ |
| 78 | .filename = __FILE__, \ |
| 79 | .format = (fmt), \ |
| 80 | .lineno = __LINE__, \ |
Jason Baron | 9049fc7 | 2016-08-03 13:46:39 -0700 | [diff] [blame] | 81 | .flags = _DPRINTK_FLAGS_DEFAULT, \ |
| 82 | dd_key_init(key, init) \ |
Jason Baron | 07613b0 | 2011-10-04 14:13:15 -0700 | [diff] [blame] | 83 | } |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 84 | |
Jason Baron | 9049fc7 | 2016-08-03 13:46:39 -0700 | [diff] [blame] | 85 | #ifdef HAVE_JUMP_LABEL |
| 86 | |
| 87 | #define dd_key_init(key, init) key = (init) |
| 88 | |
| 89 | #ifdef DEBUG |
| 90 | #define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \ |
| 91 | DEFINE_DYNAMIC_DEBUG_METADATA_KEY(name, fmt, .key.dd_key_true, \ |
| 92 | (STATIC_KEY_TRUE_INIT)) |
| 93 | |
| 94 | #define DYNAMIC_DEBUG_BRANCH(descriptor) \ |
| 95 | static_branch_likely(&descriptor.key.dd_key_true) |
| 96 | #else |
| 97 | #define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \ |
| 98 | DEFINE_DYNAMIC_DEBUG_METADATA_KEY(name, fmt, .key.dd_key_false, \ |
| 99 | (STATIC_KEY_FALSE_INIT)) |
| 100 | |
| 101 | #define DYNAMIC_DEBUG_BRANCH(descriptor) \ |
| 102 | static_branch_unlikely(&descriptor.key.dd_key_false) |
| 103 | #endif |
| 104 | |
| 105 | #else |
| 106 | |
| 107 | #define dd_key_init(key, init) |
| 108 | |
| 109 | #define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \ |
| 110 | DEFINE_DYNAMIC_DEBUG_METADATA_KEY(name, fmt, 0, 0) |
| 111 | |
| 112 | #ifdef DEBUG |
| 113 | #define DYNAMIC_DEBUG_BRANCH(descriptor) \ |
| 114 | likely(descriptor.flags & _DPRINTK_FLAGS_PRINT) |
| 115 | #else |
| 116 | #define DYNAMIC_DEBUG_BRANCH(descriptor) \ |
| 117 | unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT) |
| 118 | #endif |
| 119 | |
| 120 | #endif |
| 121 | |
Jason Baron | 07613b0 | 2011-10-04 14:13:15 -0700 | [diff] [blame] | 122 | #define dynamic_pr_debug(fmt, ...) \ |
| 123 | do { \ |
| 124 | DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \ |
Jason Baron | 9049fc7 | 2016-08-03 13:46:39 -0700 | [diff] [blame] | 125 | if (DYNAMIC_DEBUG_BRANCH(descriptor)) \ |
Jason Baron | 07613b0 | 2011-10-04 14:13:15 -0700 | [diff] [blame] | 126 | __dynamic_pr_debug(&descriptor, pr_fmt(fmt), \ |
| 127 | ##__VA_ARGS__); \ |
| 128 | } while (0) |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 129 | |
Jason Baron | 07613b0 | 2011-10-04 14:13:15 -0700 | [diff] [blame] | 130 | #define dynamic_dev_dbg(dev, fmt, ...) \ |
| 131 | do { \ |
Jim Cromie | 87e6f96 | 2011-12-19 17:11:13 -0500 | [diff] [blame] | 132 | DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \ |
Jason Baron | 9049fc7 | 2016-08-03 13:46:39 -0700 | [diff] [blame] | 133 | if (DYNAMIC_DEBUG_BRANCH(descriptor)) \ |
Jason Baron | 07613b0 | 2011-10-04 14:13:15 -0700 | [diff] [blame] | 134 | __dynamic_dev_dbg(&descriptor, dev, fmt, \ |
| 135 | ##__VA_ARGS__); \ |
| 136 | } while (0) |
| 137 | |
| 138 | #define dynamic_netdev_dbg(dev, fmt, ...) \ |
| 139 | do { \ |
| 140 | DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \ |
Jason Baron | 9049fc7 | 2016-08-03 13:46:39 -0700 | [diff] [blame] | 141 | if (DYNAMIC_DEBUG_BRANCH(descriptor)) \ |
Jason Baron | 07613b0 | 2011-10-04 14:13:15 -0700 | [diff] [blame] | 142 | __dynamic_netdev_dbg(&descriptor, dev, fmt, \ |
| 143 | ##__VA_ARGS__); \ |
| 144 | } while (0) |
Jason Baron | ffa10cb | 2011-08-11 14:36:48 -0400 | [diff] [blame] | 145 | |
Vladimir Kondratiev | 7a55561 | 2012-12-05 16:48:27 -0500 | [diff] [blame] | 146 | #define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \ |
| 147 | groupsize, buf, len, ascii) \ |
| 148 | do { \ |
| 149 | DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, \ |
| 150 | __builtin_constant_p(prefix_str) ? prefix_str : "hexdump");\ |
Jason Baron | 9049fc7 | 2016-08-03 13:46:39 -0700 | [diff] [blame] | 151 | if (DYNAMIC_DEBUG_BRANCH(descriptor)) \ |
Vladimir Kondratiev | 7a55561 | 2012-12-05 16:48:27 -0500 | [diff] [blame] | 152 | print_hex_dump(KERN_DEBUG, prefix_str, \ |
| 153 | prefix_type, rowsize, groupsize, \ |
| 154 | buf, len, ascii); \ |
| 155 | } while (0) |
| 156 | |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 157 | #else |
| 158 | |
Jim Cromie | b48420c | 2012-04-27 14:30:35 -0600 | [diff] [blame] | 159 | #include <linux/string.h> |
| 160 | #include <linux/errno.h> |
| 161 | |
Yehuda Sadeh | ff49d74 | 2010-07-03 13:07:35 +1000 | [diff] [blame] | 162 | static inline int ddebug_remove_module(const char *mod) |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 163 | { |
| 164 | return 0; |
| 165 | } |
| 166 | |
Jim Cromie | b48420c | 2012-04-27 14:30:35 -0600 | [diff] [blame] | 167 | static inline int ddebug_dyndbg_module_param_cb(char *param, char *val, |
| 168 | const char *modname) |
| 169 | { |
| 170 | if (strstr(param, "dyndbg")) { |
Jim Cromie | 516cf1b | 2012-05-01 05:23:12 -0600 | [diff] [blame] | 171 | /* avoid pr_warn(), which wants pr_fmt() fully defined */ |
| 172 | printk(KERN_WARNING "dyndbg param is supported only in " |
Jim Cromie | b48420c | 2012-04-27 14:30:35 -0600 | [diff] [blame] | 173 | "CONFIG_DYNAMIC_DEBUG builds\n"); |
| 174 | return 0; /* allow and ignore */ |
| 175 | } |
| 176 | return -EINVAL; |
| 177 | } |
| 178 | |
Joe Perches | 00b5586 | 2009-12-14 18:00:14 -0800 | [diff] [blame] | 179 | #define dynamic_pr_debug(fmt, ...) \ |
| 180 | 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] | 181 | #define dynamic_dev_dbg(dev, fmt, ...) \ |
Joe Perches | 00b5586 | 2009-12-14 18:00:14 -0800 | [diff] [blame] | 182 | do { if (0) dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); } while (0) |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 183 | #endif |
| 184 | |
| 185 | #endif |