1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _DYNAMIC_DEBUG_H
3 #define _DYNAMIC_DEBUG_H
4 
5 #if defined(CONFIG_JUMP_LABEL)
6 #include <linux/jump_label.h>
7 #endif
8 
9 #include <linux/build_bug.h>
10 
11 /*
12  * An instance of this structure is created in a special
13  * ELF section at every dynamic debug callsite.  At runtime,
14  * the special section is treated as an array of these.
15  */
16 struct _ddebug {
17 	/*
18 	 * These fields are used to drive the user interface
19 	 * for selecting and displaying debug callsites.
20 	 */
21 	const char *modname;
22 	const char *function;
23 	const char *filename;
24 	const char *format;
25 	unsigned int lineno:18;
26 #define CLS_BITS 6
27 	unsigned int class_id:CLS_BITS;
28 #define _DPRINTK_CLASS_DFLT		((1 << CLS_BITS) - 1)
29 	/*
30 	 * The flags field controls the behaviour at the callsite.
31 	 * The bits here are changed dynamically when the user
32 	 * writes commands to <debugfs>/dynamic_debug/control
33 	 */
34 #define _DPRINTK_FLAGS_NONE	0
35 #define _DPRINTK_FLAGS_PRINT	(1<<0) /* printk() a message using the format */
36 #define _DPRINTK_FLAGS_INCL_MODNAME	(1<<1)
37 #define _DPRINTK_FLAGS_INCL_FUNCNAME	(1<<2)
38 #define _DPRINTK_FLAGS_INCL_LINENO	(1<<3)
39 #define _DPRINTK_FLAGS_INCL_TID		(1<<4)
40 
41 #define _DPRINTK_FLAGS_INCL_ANY		\
42 	(_DPRINTK_FLAGS_INCL_MODNAME | _DPRINTK_FLAGS_INCL_FUNCNAME |\
43 	 _DPRINTK_FLAGS_INCL_LINENO  | _DPRINTK_FLAGS_INCL_TID)
44 
45 #if defined DEBUG
46 #define _DPRINTK_FLAGS_DEFAULT _DPRINTK_FLAGS_PRINT
47 #else
48 #define _DPRINTK_FLAGS_DEFAULT 0
49 #endif
50 	unsigned int flags:8;
51 #ifdef CONFIG_JUMP_LABEL
52 	union {
53 		struct static_key_true dd_key_true;
54 		struct static_key_false dd_key_false;
55 	} key;
56 #endif
57 } __attribute__((aligned(8)));
58 
59 enum class_map_type {
60 	DD_CLASS_TYPE_DISJOINT_BITS,
61 	/**
62 	 * DD_CLASS_TYPE_DISJOINT_BITS: classes are independent, one per bit.
63 	 * expecting hex input. Built for drm.debug, basis for other types.
64 	 */
65 	DD_CLASS_TYPE_LEVEL_NUM,
66 	/**
67 	 * DD_CLASS_TYPE_LEVEL_NUM: input is numeric level, 0-N.
68 	 * N turns on just bits N-1 .. 0, so N=0 turns all bits off.
69 	 */
70 	DD_CLASS_TYPE_DISJOINT_NAMES,
71 	/**
72 	 * DD_CLASS_TYPE_DISJOINT_NAMES: input is a CSV of [+-]CLASS_NAMES,
73 	 * classes are independent, like _DISJOINT_BITS.
74 	 */
75 	DD_CLASS_TYPE_LEVEL_NAMES,
76 	/**
77 	 * DD_CLASS_TYPE_LEVEL_NAMES: input is a CSV of [+-]CLASS_NAMES,
78 	 * intended for names like: INFO,DEBUG,TRACE, with a module prefix
79 	 * avoid EMERG,ALERT,CRIT,ERR,WARNING: they're not debug
80 	 */
81 };
82 
83 struct ddebug_class_map {
84 	struct list_head link;
85 	struct module *mod;
86 	const char *mod_name;	/* needed for builtins */
87 	const char **class_names;
88 	const int length;
89 	const int base;		/* index of 1st .class_id, allows split/shared space */
90 	enum class_map_type map_type;
91 };
92 
93 /**
94  * DECLARE_DYNDBG_CLASSMAP - declare classnames known by a module
95  * @_var:   a struct ddebug_class_map, passed to module_param_cb
96  * @_type:  enum class_map_type, chooses bits/verbose, numeric/symbolic
97  * @_base:  offset of 1st class-name. splits .class_id space
98  * @classes: class-names used to control class'd prdbgs
99  */
100 #define DECLARE_DYNDBG_CLASSMAP(_var, _maptype, _base, ...)		\
101 	static const char *_var##_classnames[] = { __VA_ARGS__ };	\
102 	static struct ddebug_class_map __aligned(8) __used		\
103 		__section("__dyndbg_classes") _var = {			\
104 		.mod = THIS_MODULE,					\
105 		.mod_name = KBUILD_MODNAME,				\
106 		.base = _base,						\
107 		.map_type = _maptype,					\
108 		.length = NUM_TYPE_ARGS(char*, __VA_ARGS__),		\
109 		.class_names = _var##_classnames,			\
110 	}
111 #define NUM_TYPE_ARGS(eltype, ...)				\
112         (sizeof((eltype[]){__VA_ARGS__}) / sizeof(eltype))
113 
114 /* encapsulate linker provided built-in (or module) dyndbg data */
115 struct _ddebug_info {
116 	struct _ddebug *descs;
117 	struct ddebug_class_map *classes;
118 	unsigned int num_descs;
119 	unsigned int num_classes;
120 };
121 
122 struct ddebug_class_param {
123 	union {
124 		unsigned long *bits;
125 		unsigned int *lvl;
126 	};
127 	char flags[8];
128 	const struct ddebug_class_map *map;
129 };
130 
131 #if defined(CONFIG_DYNAMIC_DEBUG_CORE)
132 
133 int ddebug_add_module(struct _ddebug_info *dyndbg, const char *modname);
134 
135 extern int ddebug_remove_module(const char *mod_name);
136 extern __printf(2, 3)
137 void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...);
138 
139 extern int ddebug_dyndbg_module_param_cb(char *param, char *val,
140 					const char *modname);
141 
142 struct device;
143 
144 extern __printf(3, 4)
145 void __dynamic_dev_dbg(struct _ddebug *descriptor, const struct device *dev,
146 		       const char *fmt, ...);
147 
148 struct net_device;
149 
150 extern __printf(3, 4)
151 void __dynamic_netdev_dbg(struct _ddebug *descriptor,
152 			  const struct net_device *dev,
153 			  const char *fmt, ...);
154 
155 struct ib_device;
156 
157 extern __printf(3, 4)
158 void __dynamic_ibdev_dbg(struct _ddebug *descriptor,
159 			 const struct ib_device *ibdev,
160 			 const char *fmt, ...);
161 
162 #define DEFINE_DYNAMIC_DEBUG_METADATA_CLS(name, cls, fmt)	\
163 	static struct _ddebug  __aligned(8)			\
164 	__section("__dyndbg") name = {				\
165 		.modname = KBUILD_MODNAME,			\
166 		.function = __func__,				\
167 		.filename = __FILE__,				\
168 		.format = (fmt),				\
169 		.lineno = __LINE__,				\
170 		.flags = _DPRINTK_FLAGS_DEFAULT,		\
171 		.class_id = cls,				\
172 		_DPRINTK_KEY_INIT				\
173 	};							\
174 	BUILD_BUG_ON_MSG(cls > _DPRINTK_CLASS_DFLT,		\
175 			 "classid value overflow")
176 
177 #define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt)		\
178 	DEFINE_DYNAMIC_DEBUG_METADATA_CLS(name, _DPRINTK_CLASS_DFLT, fmt)
179 
180 #ifdef CONFIG_JUMP_LABEL
181 
182 #ifdef DEBUG
183 
184 #define _DPRINTK_KEY_INIT .key.dd_key_true = (STATIC_KEY_TRUE_INIT)
185 
186 #define DYNAMIC_DEBUG_BRANCH(descriptor) \
187 	static_branch_likely(&descriptor.key.dd_key_true)
188 #else
189 #define _DPRINTK_KEY_INIT .key.dd_key_false = (STATIC_KEY_FALSE_INIT)
190 
191 #define DYNAMIC_DEBUG_BRANCH(descriptor) \
192 	static_branch_unlikely(&descriptor.key.dd_key_false)
193 #endif
194 
195 #else /* !CONFIG_JUMP_LABEL */
196 
197 #define _DPRINTK_KEY_INIT
198 
199 #ifdef DEBUG
200 #define DYNAMIC_DEBUG_BRANCH(descriptor) \
201 	likely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
202 #else
203 #define DYNAMIC_DEBUG_BRANCH(descriptor) \
204 	unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
205 #endif
206 
207 #endif /* CONFIG_JUMP_LABEL */
208 
209 /*
210  * Factory macros: ($prefix)dynamic_func_call($suffix)
211  *
212  * Lower layer (with __ prefix) gets the callsite metadata, and wraps
213  * the func inside a debug-branch/static-key construct.  Upper layer
214  * (with _ prefix) does the UNIQUE_ID once, so that lower can ref the
215  * name/label multiple times, and tie the elements together.
216  * Multiple flavors:
217  * (|_cls):	adds in _DPRINT_CLASS_DFLT as needed
218  * (|_no_desc):	former gets callsite descriptor as 1st arg (for prdbgs)
219  */
220 #define __dynamic_func_call_cls(id, cls, fmt, func, ...) do {	\
221 	DEFINE_DYNAMIC_DEBUG_METADATA_CLS(id, cls, fmt);	\
222 	if (DYNAMIC_DEBUG_BRANCH(id))				\
223 		func(&id, ##__VA_ARGS__);			\
224 } while (0)
225 #define __dynamic_func_call(id, fmt, func, ...)				\
226 	__dynamic_func_call_cls(id, _DPRINTK_CLASS_DFLT, fmt,		\
227 				func, ##__VA_ARGS__)
228 
229 #define __dynamic_func_call_cls_no_desc(id, cls, fmt, func, ...) do {	\
230 	DEFINE_DYNAMIC_DEBUG_METADATA_CLS(id, cls, fmt);		\
231 	if (DYNAMIC_DEBUG_BRANCH(id))					\
232 		func(__VA_ARGS__);					\
233 } while (0)
234 #define __dynamic_func_call_no_desc(id, fmt, func, ...)			\
235 	__dynamic_func_call_cls_no_desc(id, _DPRINTK_CLASS_DFLT,	\
236 					fmt, func, ##__VA_ARGS__)
237 
238 /*
239  * "Factory macro" for generating a call to func, guarded by a
240  * DYNAMIC_DEBUG_BRANCH. The dynamic debug descriptor will be
241  * initialized using the fmt argument. The function will be called with
242  * the address of the descriptor as first argument, followed by all
243  * the varargs. Note that fmt is repeated in invocations of this
244  * macro.
245  */
246 #define _dynamic_func_call_cls(cls, fmt, func, ...)			\
247 	__dynamic_func_call_cls(__UNIQUE_ID(ddebug), cls, fmt, func, ##__VA_ARGS__)
248 #define _dynamic_func_call(fmt, func, ...)				\
249 	_dynamic_func_call_cls(_DPRINTK_CLASS_DFLT, fmt, func, ##__VA_ARGS__)
250 
251 /*
252  * A variant that does the same, except that the descriptor is not
253  * passed as the first argument to the function; it is only called
254  * with precisely the macro's varargs.
255  */
256 #define _dynamic_func_call_cls_no_desc(cls, fmt, func, ...)		\
257 	__dynamic_func_call_cls_no_desc(__UNIQUE_ID(ddebug), cls, fmt,	\
258 					func, ##__VA_ARGS__)
259 #define _dynamic_func_call_no_desc(fmt, func, ...)			\
260 	_dynamic_func_call_cls_no_desc(_DPRINTK_CLASS_DFLT, fmt,	\
261 				       func, ##__VA_ARGS__)
262 
263 #define dynamic_pr_debug_cls(cls, fmt, ...)				\
264 	_dynamic_func_call_cls(cls, fmt, __dynamic_pr_debug,		\
265 			   pr_fmt(fmt), ##__VA_ARGS__)
266 
267 #define dynamic_pr_debug(fmt, ...)				\
268 	_dynamic_func_call(fmt, __dynamic_pr_debug,		\
269 			   pr_fmt(fmt), ##__VA_ARGS__)
270 
271 #define dynamic_dev_dbg(dev, fmt, ...)				\
272 	_dynamic_func_call(fmt, __dynamic_dev_dbg, 		\
273 			   dev, fmt, ##__VA_ARGS__)
274 
275 #define dynamic_netdev_dbg(dev, fmt, ...)			\
276 	_dynamic_func_call(fmt, __dynamic_netdev_dbg,		\
277 			   dev, fmt, ##__VA_ARGS__)
278 
279 #define dynamic_ibdev_dbg(dev, fmt, ...)			\
280 	_dynamic_func_call(fmt, __dynamic_ibdev_dbg,		\
281 			   dev, fmt, ##__VA_ARGS__)
282 
283 #define dynamic_hex_dump(prefix_str, prefix_type, rowsize,		\
284 			 groupsize, buf, len, ascii)			\
285 	_dynamic_func_call_no_desc(__builtin_constant_p(prefix_str) ? prefix_str : "hexdump", \
286 				   print_hex_dump,			\
287 				   KERN_DEBUG, prefix_str, prefix_type,	\
288 				   rowsize, groupsize, buf, len, ascii)
289 
290 struct kernel_param;
291 int param_set_dyndbg_classes(const char *instr, const struct kernel_param *kp);
292 int param_get_dyndbg_classes(char *buffer, const struct kernel_param *kp);
293 
294 /* for test only, generally expect drm.debug style macro wrappers */
295 #define __pr_debug_cls(cls, fmt, ...) do {			\
296 	BUILD_BUG_ON_MSG(!__builtin_constant_p(cls),		\
297 			 "expecting constant class int/enum");	\
298 	dynamic_pr_debug_cls(cls, fmt, ##__VA_ARGS__);		\
299 	} while (0)
300 
301 #else /* !CONFIG_DYNAMIC_DEBUG_CORE */
302 
303 #include <linux/string.h>
304 #include <linux/errno.h>
305 #include <linux/printk.h>
306 
ddebug_add_module(struct _ddebug_info * dinfo,const char * modname)307 static inline int ddebug_add_module(struct _ddebug_info *dinfo, const char *modname)
308 {
309 	return 0;
310 }
311 
ddebug_remove_module(const char * mod)312 static inline int ddebug_remove_module(const char *mod)
313 {
314 	return 0;
315 }
316 
ddebug_dyndbg_module_param_cb(char * param,char * val,const char * modname)317 static inline int ddebug_dyndbg_module_param_cb(char *param, char *val,
318 						const char *modname)
319 {
320 	if (!strcmp(param, "dyndbg")) {
321 		/* avoid pr_warn(), which wants pr_fmt() fully defined */
322 		printk(KERN_WARNING "dyndbg param is supported only in "
323 			"CONFIG_DYNAMIC_DEBUG builds\n");
324 		return 0; /* allow and ignore */
325 	}
326 	return -EINVAL;
327 }
328 
329 #define dynamic_pr_debug(fmt, ...)					\
330 	do { if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); } while (0)
331 #define dynamic_dev_dbg(dev, fmt, ...)					\
332 	do { if (0) dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); } while (0)
333 #define dynamic_hex_dump(prefix_str, prefix_type, rowsize,		\
334 			 groupsize, buf, len, ascii)			\
335 	do { if (0)							\
336 		print_hex_dump(KERN_DEBUG, prefix_str, prefix_type,	\
337 				rowsize, groupsize, buf, len, ascii);	\
338 	} while (0)
339 
340 struct kernel_param;
param_set_dyndbg_classes(const char * instr,const struct kernel_param * kp)341 static inline int param_set_dyndbg_classes(const char *instr, const struct kernel_param *kp)
342 { return 0; }
param_get_dyndbg_classes(char * buffer,const struct kernel_param * kp)343 static inline int param_get_dyndbg_classes(char *buffer, const struct kernel_param *kp)
344 { return 0; }
345 
346 #endif /* !CONFIG_DYNAMIC_DEBUG_CORE */
347 
348 extern const struct kernel_param_ops param_ops_dyndbg_classes;
349 
350 #endif
351