1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _DYNAMIC_DEBUG_H
3*4882a593Smuzhiyun #define _DYNAMIC_DEBUG_H
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun #if defined(CONFIG_JUMP_LABEL)
6*4882a593Smuzhiyun #include <linux/jump_label.h>
7*4882a593Smuzhiyun #endif
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun /*
10*4882a593Smuzhiyun * An instance of this structure is created in a special
11*4882a593Smuzhiyun * ELF section at every dynamic debug callsite. At runtime,
12*4882a593Smuzhiyun * the special section is treated as an array of these.
13*4882a593Smuzhiyun */
14*4882a593Smuzhiyun struct _ddebug {
15*4882a593Smuzhiyun /*
16*4882a593Smuzhiyun * These fields are used to drive the user interface
17*4882a593Smuzhiyun * for selecting and displaying debug callsites.
18*4882a593Smuzhiyun */
19*4882a593Smuzhiyun const char *modname;
20*4882a593Smuzhiyun const char *function;
21*4882a593Smuzhiyun const char *filename;
22*4882a593Smuzhiyun const char *format;
23*4882a593Smuzhiyun unsigned int lineno:18;
24*4882a593Smuzhiyun /*
25*4882a593Smuzhiyun * The flags field controls the behaviour at the callsite.
26*4882a593Smuzhiyun * The bits here are changed dynamically when the user
27*4882a593Smuzhiyun * writes commands to <debugfs>/dynamic_debug/control
28*4882a593Smuzhiyun */
29*4882a593Smuzhiyun #define _DPRINTK_FLAGS_NONE 0
30*4882a593Smuzhiyun #define _DPRINTK_FLAGS_PRINT (1<<0) /* printk() a message using the format */
31*4882a593Smuzhiyun #define _DPRINTK_FLAGS_INCL_MODNAME (1<<1)
32*4882a593Smuzhiyun #define _DPRINTK_FLAGS_INCL_FUNCNAME (1<<2)
33*4882a593Smuzhiyun #define _DPRINTK_FLAGS_INCL_LINENO (1<<3)
34*4882a593Smuzhiyun #define _DPRINTK_FLAGS_INCL_TID (1<<4)
35*4882a593Smuzhiyun #if defined DEBUG
36*4882a593Smuzhiyun #define _DPRINTK_FLAGS_DEFAULT _DPRINTK_FLAGS_PRINT
37*4882a593Smuzhiyun #else
38*4882a593Smuzhiyun #define _DPRINTK_FLAGS_DEFAULT 0
39*4882a593Smuzhiyun #endif
40*4882a593Smuzhiyun unsigned int flags:8;
41*4882a593Smuzhiyun #ifdef CONFIG_JUMP_LABEL
42*4882a593Smuzhiyun union {
43*4882a593Smuzhiyun struct static_key_true dd_key_true;
44*4882a593Smuzhiyun struct static_key_false dd_key_false;
45*4882a593Smuzhiyun } key;
46*4882a593Smuzhiyun #endif
47*4882a593Smuzhiyun } __attribute__((aligned(8)));
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun #if defined(CONFIG_DYNAMIC_DEBUG_CORE)
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun int ddebug_add_module(struct _ddebug *tab, unsigned int n,
54*4882a593Smuzhiyun const char *modname);
55*4882a593Smuzhiyun extern int ddebug_remove_module(const char *mod_name);
56*4882a593Smuzhiyun extern __printf(2, 3)
57*4882a593Smuzhiyun void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...);
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun extern int ddebug_dyndbg_module_param_cb(char *param, char *val,
60*4882a593Smuzhiyun const char *modname);
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun struct device;
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun extern __printf(3, 4)
65*4882a593Smuzhiyun void __dynamic_dev_dbg(struct _ddebug *descriptor, const struct device *dev,
66*4882a593Smuzhiyun const char *fmt, ...);
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun struct net_device;
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun extern __printf(3, 4)
71*4882a593Smuzhiyun void __dynamic_netdev_dbg(struct _ddebug *descriptor,
72*4882a593Smuzhiyun const struct net_device *dev,
73*4882a593Smuzhiyun const char *fmt, ...);
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun struct ib_device;
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun extern __printf(3, 4)
78*4882a593Smuzhiyun void __dynamic_ibdev_dbg(struct _ddebug *descriptor,
79*4882a593Smuzhiyun const struct ib_device *ibdev,
80*4882a593Smuzhiyun const char *fmt, ...);
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun #define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \
83*4882a593Smuzhiyun static struct _ddebug __aligned(8) \
84*4882a593Smuzhiyun __section("__dyndbg") name = { \
85*4882a593Smuzhiyun .modname = KBUILD_MODNAME, \
86*4882a593Smuzhiyun .function = __func__, \
87*4882a593Smuzhiyun .filename = __FILE__, \
88*4882a593Smuzhiyun .format = (fmt), \
89*4882a593Smuzhiyun .lineno = __LINE__, \
90*4882a593Smuzhiyun .flags = _DPRINTK_FLAGS_DEFAULT, \
91*4882a593Smuzhiyun _DPRINTK_KEY_INIT \
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun #ifdef CONFIG_JUMP_LABEL
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun #ifdef DEBUG
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun #define _DPRINTK_KEY_INIT .key.dd_key_true = (STATIC_KEY_TRUE_INIT)
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun #define DYNAMIC_DEBUG_BRANCH(descriptor) \
101*4882a593Smuzhiyun static_branch_likely(&descriptor.key.dd_key_true)
102*4882a593Smuzhiyun #else
103*4882a593Smuzhiyun #define _DPRINTK_KEY_INIT .key.dd_key_false = (STATIC_KEY_FALSE_INIT)
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun #define DYNAMIC_DEBUG_BRANCH(descriptor) \
106*4882a593Smuzhiyun static_branch_unlikely(&descriptor.key.dd_key_false)
107*4882a593Smuzhiyun #endif
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun #else /* !CONFIG_JUMP_LABEL */
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun #define _DPRINTK_KEY_INIT
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun #ifdef DEBUG
114*4882a593Smuzhiyun #define DYNAMIC_DEBUG_BRANCH(descriptor) \
115*4882a593Smuzhiyun likely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
116*4882a593Smuzhiyun #else
117*4882a593Smuzhiyun #define DYNAMIC_DEBUG_BRANCH(descriptor) \
118*4882a593Smuzhiyun unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
119*4882a593Smuzhiyun #endif
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun #endif /* CONFIG_JUMP_LABEL */
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun #define __dynamic_func_call(id, fmt, func, ...) do { \
124*4882a593Smuzhiyun DEFINE_DYNAMIC_DEBUG_METADATA(id, fmt); \
125*4882a593Smuzhiyun if (DYNAMIC_DEBUG_BRANCH(id)) \
126*4882a593Smuzhiyun func(&id, ##__VA_ARGS__); \
127*4882a593Smuzhiyun } while (0)
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun #define __dynamic_func_call_no_desc(id, fmt, func, ...) do { \
130*4882a593Smuzhiyun DEFINE_DYNAMIC_DEBUG_METADATA(id, fmt); \
131*4882a593Smuzhiyun if (DYNAMIC_DEBUG_BRANCH(id)) \
132*4882a593Smuzhiyun func(__VA_ARGS__); \
133*4882a593Smuzhiyun } while (0)
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun /*
136*4882a593Smuzhiyun * "Factory macro" for generating a call to func, guarded by a
137*4882a593Smuzhiyun * DYNAMIC_DEBUG_BRANCH. The dynamic debug descriptor will be
138*4882a593Smuzhiyun * initialized using the fmt argument. The function will be called with
139*4882a593Smuzhiyun * the address of the descriptor as first argument, followed by all
140*4882a593Smuzhiyun * the varargs. Note that fmt is repeated in invocations of this
141*4882a593Smuzhiyun * macro.
142*4882a593Smuzhiyun */
143*4882a593Smuzhiyun #define _dynamic_func_call(fmt, func, ...) \
144*4882a593Smuzhiyun __dynamic_func_call(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__)
145*4882a593Smuzhiyun /*
146*4882a593Smuzhiyun * A variant that does the same, except that the descriptor is not
147*4882a593Smuzhiyun * passed as the first argument to the function; it is only called
148*4882a593Smuzhiyun * with precisely the macro's varargs.
149*4882a593Smuzhiyun */
150*4882a593Smuzhiyun #define _dynamic_func_call_no_desc(fmt, func, ...) \
151*4882a593Smuzhiyun __dynamic_func_call_no_desc(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__)
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun #define dynamic_pr_debug(fmt, ...) \
154*4882a593Smuzhiyun _dynamic_func_call(fmt, __dynamic_pr_debug, \
155*4882a593Smuzhiyun pr_fmt(fmt), ##__VA_ARGS__)
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun #define dynamic_dev_dbg(dev, fmt, ...) \
158*4882a593Smuzhiyun _dynamic_func_call(fmt,__dynamic_dev_dbg, \
159*4882a593Smuzhiyun dev, fmt, ##__VA_ARGS__)
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun #define dynamic_netdev_dbg(dev, fmt, ...) \
162*4882a593Smuzhiyun _dynamic_func_call(fmt, __dynamic_netdev_dbg, \
163*4882a593Smuzhiyun dev, fmt, ##__VA_ARGS__)
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun #define dynamic_ibdev_dbg(dev, fmt, ...) \
166*4882a593Smuzhiyun _dynamic_func_call(fmt, __dynamic_ibdev_dbg, \
167*4882a593Smuzhiyun dev, fmt, ##__VA_ARGS__)
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun #define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
170*4882a593Smuzhiyun groupsize, buf, len, ascii) \
171*4882a593Smuzhiyun _dynamic_func_call_no_desc(__builtin_constant_p(prefix_str) ? prefix_str : "hexdump", \
172*4882a593Smuzhiyun print_hex_dump, \
173*4882a593Smuzhiyun KERN_DEBUG, prefix_str, prefix_type, \
174*4882a593Smuzhiyun rowsize, groupsize, buf, len, ascii)
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun #else /* !CONFIG_DYNAMIC_DEBUG_CORE */
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun #include <linux/string.h>
179*4882a593Smuzhiyun #include <linux/errno.h>
180*4882a593Smuzhiyun #include <linux/printk.h>
181*4882a593Smuzhiyun
ddebug_add_module(struct _ddebug * tab,unsigned int n,const char * modname)182*4882a593Smuzhiyun static inline int ddebug_add_module(struct _ddebug *tab, unsigned int n,
183*4882a593Smuzhiyun const char *modname)
184*4882a593Smuzhiyun {
185*4882a593Smuzhiyun return 0;
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun
ddebug_remove_module(const char * mod)188*4882a593Smuzhiyun static inline int ddebug_remove_module(const char *mod)
189*4882a593Smuzhiyun {
190*4882a593Smuzhiyun return 0;
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun
ddebug_dyndbg_module_param_cb(char * param,char * val,const char * modname)193*4882a593Smuzhiyun static inline int ddebug_dyndbg_module_param_cb(char *param, char *val,
194*4882a593Smuzhiyun const char *modname)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun if (!strcmp(param, "dyndbg")) {
197*4882a593Smuzhiyun /* avoid pr_warn(), which wants pr_fmt() fully defined */
198*4882a593Smuzhiyun printk(KERN_WARNING "dyndbg param is supported only in "
199*4882a593Smuzhiyun "CONFIG_DYNAMIC_DEBUG builds\n");
200*4882a593Smuzhiyun return 0; /* allow and ignore */
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun return -EINVAL;
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun #define dynamic_pr_debug(fmt, ...) \
206*4882a593Smuzhiyun do { if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); } while (0)
207*4882a593Smuzhiyun #define dynamic_dev_dbg(dev, fmt, ...) \
208*4882a593Smuzhiyun do { if (0) dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); } while (0)
209*4882a593Smuzhiyun #define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \
210*4882a593Smuzhiyun groupsize, buf, len, ascii) \
211*4882a593Smuzhiyun do { if (0) \
212*4882a593Smuzhiyun print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, \
213*4882a593Smuzhiyun rowsize, groupsize, buf, len, ascii); \
214*4882a593Smuzhiyun } while (0)
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun #endif /* !CONFIG_DYNAMIC_DEBUG_CORE */
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun #endif
219