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