1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _DYNAMIC_DEBUG_H
3 #define _DYNAMIC_DEBUG_H
5 #if defined(CONFIG_JUMP_LABEL)
6 #include <linux/jump_label.h>
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.
16 * These fields are used to drive the user interface
17 * for selecting and displaying debug callsites.
23 unsigned int lineno:18;
25 * The flags field controls the behaviour at the callsite.
26 * The bits here are changed dynamically when the user
27 * writes commands to <debugfs>/dynamic_debug/control
29 #define _DPRINTK_FLAGS_NONE 0
30 #define _DPRINTK_FLAGS_PRINT (1<<0) /* printk() a message using the format */
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)
36 #define _DPRINTK_FLAGS_DEFAULT _DPRINTK_FLAGS_PRINT
38 #define _DPRINTK_FLAGS_DEFAULT 0
41 #ifdef CONFIG_JUMP_LABEL
43 struct static_key_true dd_key_true;
44 struct static_key_false dd_key_false;
47 } __attribute__((aligned(8)));
51 #if defined(CONFIG_DYNAMIC_DEBUG)
52 int ddebug_add_module(struct _ddebug *tab, unsigned int n,
54 extern int ddebug_remove_module(const char *mod_name);
56 void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...);
58 extern int ddebug_dyndbg_module_param_cb(char *param, char *val,
64 void __dynamic_dev_dbg(struct _ddebug *descriptor, const struct device *dev,
65 const char *fmt, ...);
70 void __dynamic_netdev_dbg(struct _ddebug *descriptor,
71 const struct net_device *dev,
72 const char *fmt, ...);
74 #define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \
75 static struct _ddebug __aligned(8) \
76 __attribute__((section("__verbose"))) name = { \
77 .modname = KBUILD_MODNAME, \
78 .function = __func__, \
79 .filename = __FILE__, \
82 .flags = _DPRINTK_FLAGS_DEFAULT, \
86 #ifdef CONFIG_JUMP_LABEL
90 #define _DPRINTK_KEY_INIT .key.dd_key_true = (STATIC_KEY_TRUE_INIT)
92 #define DYNAMIC_DEBUG_BRANCH(descriptor) \
93 static_branch_likely(&descriptor.key.dd_key_true)
95 #define _DPRINTK_KEY_INIT .key.dd_key_false = (STATIC_KEY_FALSE_INIT)
97 #define DYNAMIC_DEBUG_BRANCH(descriptor) \
98 static_branch_unlikely(&descriptor.key.dd_key_false)
101 #else /* !HAVE_JUMP_LABEL */
103 #define _DPRINTK_KEY_INIT
106 #define DYNAMIC_DEBUG_BRANCH(descriptor) \
107 likely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
109 #define DYNAMIC_DEBUG_BRANCH(descriptor) \
110 unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
115 #define __dynamic_func_call(id, fmt, func, ...) do { \
116 DEFINE_DYNAMIC_DEBUG_METADATA(id, fmt); \
117 if (DYNAMIC_DEBUG_BRANCH(id)) \
118 func(&id, ##__VA_ARGS__); \
121 #define __dynamic_func_call_no_desc(id, fmt, func, ...) do { \
122 DEFINE_DYNAMIC_DEBUG_METADATA(id, fmt); \
123 if (DYNAMIC_DEBUG_BRANCH(id)) \
128 * "Factory macro" for generating a call to func, guarded by a
129 * DYNAMIC_DEBUG_BRANCH. The dynamic debug decriptor will be
130 * initialized using the fmt argument. The function will be called with
131 * the address of the descriptor as first argument, followed by all
132 * the varargs. Note that fmt is repeated in invocations of this
135 #define _dynamic_func_call(fmt, func, ...) \
136 __dynamic_func_call(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__)
138 * A variant that does the same, except that the descriptor is not
139 * passed as the first argument to the function; it is only called
140 * with precisely the macro's varargs.
142 #define _dynamic_func_call_no_desc(fmt, func, ...) \
143 __dynamic_func_call_no_desc(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__)
145 #define dynamic_pr_debug(fmt, ...) \
146 _dynamic_func_call(fmt, __dynamic_pr_debug, \
147 pr_fmt(fmt), ##__VA_ARGS__)
149 #define dynamic_dev_dbg(dev, fmt, ...) \
150 _dynamic_func_call(fmt,__dynamic_dev_dbg, \
151 dev, fmt, ##__VA_ARGS__)
153 #define dynamic_netdev_dbg(dev, fmt, ...) \
154 _dynamic_func_call(fmt, __dynamic_netdev_dbg, \
155 dev, fmt, ##__VA_ARGS__)
157 #define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
158 groupsize, buf, len, ascii) \
159 _dynamic_func_call_no_desc(__builtin_constant_p(prefix_str) ? prefix_str : "hexdump", \
161 KERN_DEBUG, prefix_str, prefix_type, \
162 rowsize, groupsize, buf, len, ascii)
166 #include <linux/string.h>
167 #include <linux/errno.h>
169 static inline int ddebug_add_module(struct _ddebug *tab, unsigned int n,
175 static inline int ddebug_remove_module(const char *mod)
180 static inline int ddebug_dyndbg_module_param_cb(char *param, char *val,
183 if (strstr(param, "dyndbg")) {
184 /* avoid pr_warn(), which wants pr_fmt() fully defined */
185 printk(KERN_WARNING "dyndbg param is supported only in "
186 "CONFIG_DYNAMIC_DEBUG builds\n");
187 return 0; /* allow and ignore */
192 #define dynamic_pr_debug(fmt, ...) \
193 do { if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); } while (0)
194 #define dynamic_dev_dbg(dev, fmt, ...) \
195 do { if (0) dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); } while (0)