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_INCL_ANY \
37 (_DPRINTK_FLAGS_INCL_MODNAME | _DPRINTK_FLAGS_INCL_FUNCNAME |\
38 _DPRINTK_FLAGS_INCL_LINENO | _DPRINTK_FLAGS_INCL_TID)
41 #define _DPRINTK_FLAGS_DEFAULT _DPRINTK_FLAGS_PRINT
43 #define _DPRINTK_FLAGS_DEFAULT 0
46 #ifdef CONFIG_JUMP_LABEL
48 struct static_key_true dd_key_true;
49 struct static_key_false dd_key_false;
52 } __attribute__((aligned(8)));
56 #if defined(CONFIG_DYNAMIC_DEBUG_CORE)
58 int ddebug_add_module(struct _ddebug *tab, unsigned int n,
60 extern int ddebug_remove_module(const char *mod_name);
62 void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...);
64 extern int ddebug_dyndbg_module_param_cb(char *param, char *val,
70 void __dynamic_dev_dbg(struct _ddebug *descriptor, const struct device *dev,
71 const char *fmt, ...);
76 void __dynamic_netdev_dbg(struct _ddebug *descriptor,
77 const struct net_device *dev,
78 const char *fmt, ...);
83 void __dynamic_ibdev_dbg(struct _ddebug *descriptor,
84 const struct ib_device *ibdev,
85 const char *fmt, ...);
87 #define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \
88 static struct _ddebug __aligned(8) \
89 __section("__dyndbg") name = { \
90 .modname = KBUILD_MODNAME, \
91 .function = __func__, \
92 .filename = __FILE__, \
95 .flags = _DPRINTK_FLAGS_DEFAULT, \
99 #ifdef CONFIG_JUMP_LABEL
103 #define _DPRINTK_KEY_INIT .key.dd_key_true = (STATIC_KEY_TRUE_INIT)
105 #define DYNAMIC_DEBUG_BRANCH(descriptor) \
106 static_branch_likely(&descriptor.key.dd_key_true)
108 #define _DPRINTK_KEY_INIT .key.dd_key_false = (STATIC_KEY_FALSE_INIT)
110 #define DYNAMIC_DEBUG_BRANCH(descriptor) \
111 static_branch_unlikely(&descriptor.key.dd_key_false)
114 #else /* !CONFIG_JUMP_LABEL */
116 #define _DPRINTK_KEY_INIT
119 #define DYNAMIC_DEBUG_BRANCH(descriptor) \
120 likely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
122 #define DYNAMIC_DEBUG_BRANCH(descriptor) \
123 unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
126 #endif /* CONFIG_JUMP_LABEL */
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__); \
134 #define __dynamic_func_call_no_desc(id, fmt, func, ...) do { \
135 DEFINE_DYNAMIC_DEBUG_METADATA(id, fmt); \
136 if (DYNAMIC_DEBUG_BRANCH(id)) \
141 * "Factory macro" for generating a call to func, guarded by a
142 * DYNAMIC_DEBUG_BRANCH. The dynamic debug descriptor will be
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
148 #define _dynamic_func_call(fmt, func, ...) \
149 __dynamic_func_call(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__)
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.
155 #define _dynamic_func_call_no_desc(fmt, func, ...) \
156 __dynamic_func_call_no_desc(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__)
158 #define dynamic_pr_debug(fmt, ...) \
159 _dynamic_func_call(fmt, __dynamic_pr_debug, \
160 pr_fmt(fmt), ##__VA_ARGS__)
162 #define dynamic_dev_dbg(dev, fmt, ...) \
163 _dynamic_func_call(fmt,__dynamic_dev_dbg, \
164 dev, fmt, ##__VA_ARGS__)
166 #define dynamic_netdev_dbg(dev, fmt, ...) \
167 _dynamic_func_call(fmt, __dynamic_netdev_dbg, \
168 dev, fmt, ##__VA_ARGS__)
170 #define dynamic_ibdev_dbg(dev, fmt, ...) \
171 _dynamic_func_call(fmt, __dynamic_ibdev_dbg, \
172 dev, fmt, ##__VA_ARGS__)
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", \
178 KERN_DEBUG, prefix_str, prefix_type, \
179 rowsize, groupsize, buf, len, ascii)
181 #else /* !CONFIG_DYNAMIC_DEBUG_CORE */
183 #include <linux/string.h>
184 #include <linux/errno.h>
185 #include <linux/printk.h>
187 static inline int ddebug_add_module(struct _ddebug *tab, unsigned int n,
193 static inline int ddebug_remove_module(const char *mod)
198 static inline int ddebug_dyndbg_module_param_cb(char *param, char *val,
201 if (!strcmp(param, "dyndbg")) {
202 /* avoid pr_warn(), which wants pr_fmt() fully defined */
203 printk(KERN_WARNING "dyndbg param is supported only in "
204 "CONFIG_DYNAMIC_DEBUG builds\n");
205 return 0; /* allow and ignore */
210 #define dynamic_pr_debug(fmt, ...) \
211 do { if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); } while (0)
212 #define dynamic_dev_dbg(dev, fmt, ...) \
213 do { if (0) dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); } while (0)
214 #define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
215 groupsize, buf, len, ascii) \
217 print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, \
218 rowsize, groupsize, buf, len, ascii); \
221 #endif /* !CONFIG_DYNAMIC_DEBUG_CORE */