xref: /linux-6.15/include/linux/dynamic_debug.h (revision ceabef7d)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2e9d376f0SJason Baron #ifndef _DYNAMIC_DEBUG_H
3e9d376f0SJason Baron #define _DYNAMIC_DEBUG_H
4e9d376f0SJason Baron 
5e9666d10SMasahiro Yamada #if defined(CONFIG_JUMP_LABEL)
69049fc74SJason Baron #include <linux/jump_label.h>
79049fc74SJason Baron #endif
89049fc74SJason Baron 
9e9d376f0SJason Baron /*
10e9d376f0SJason Baron  * An instance of this structure is created in a special
11e9d376f0SJason Baron  * ELF section at every dynamic debug callsite.  At runtime,
12e9d376f0SJason Baron  * the special section is treated as an array of these.
13e9d376f0SJason Baron  */
14e9d376f0SJason Baron struct _ddebug {
15e9d376f0SJason Baron 	/*
16e9d376f0SJason Baron 	 * These fields are used to drive the user interface
17e9d376f0SJason Baron 	 * for selecting and displaying debug callsites.
18e9d376f0SJason Baron 	 */
19e9d376f0SJason Baron 	const char *modname;
20e9d376f0SJason Baron 	const char *function;
21e9d376f0SJason Baron 	const char *filename;
22e9d376f0SJason Baron 	const char *format;
23e703ddaeSJim Cromie 	unsigned int lineno:18;
24e9d376f0SJason Baron 	/*
25e9d376f0SJason Baron 	 * The flags field controls the behaviour at the callsite.
26e9d376f0SJason Baron 	 * The bits here are changed dynamically when the user
272b2f68b5SFlorian Ragwitz 	 * writes commands to <debugfs>/dynamic_debug/control
28e9d376f0SJason Baron 	 */
295ca7d2a6SJim Cromie #define _DPRINTK_FLAGS_NONE	0
30e9d376f0SJason Baron #define _DPRINTK_FLAGS_PRINT	(1<<0) /* printk() a message using the format */
318ba6ebf5SBart Van Assche #define _DPRINTK_FLAGS_INCL_MODNAME	(1<<1)
328ba6ebf5SBart Van Assche #define _DPRINTK_FLAGS_INCL_FUNCNAME	(1<<2)
338ba6ebf5SBart Van Assche #define _DPRINTK_FLAGS_INCL_LINENO	(1<<3)
348ba6ebf5SBart Van Assche #define _DPRINTK_FLAGS_INCL_TID		(1<<4)
35b558c96fSJim Cromie #if defined DEBUG
36b558c96fSJim Cromie #define _DPRINTK_FLAGS_DEFAULT _DPRINTK_FLAGS_PRINT
37b558c96fSJim Cromie #else
38e9d376f0SJason Baron #define _DPRINTK_FLAGS_DEFAULT 0
39b558c96fSJim Cromie #endif
40e9d376f0SJason Baron 	unsigned int flags:8;
41e9666d10SMasahiro Yamada #ifdef CONFIG_JUMP_LABEL
429049fc74SJason Baron 	union {
439049fc74SJason Baron 		struct static_key_true dd_key_true;
449049fc74SJason Baron 		struct static_key_false dd_key_false;
459049fc74SJason Baron 	} key;
469049fc74SJason Baron #endif
47e9d376f0SJason Baron } __attribute__((aligned(8)));
48e9d376f0SJason Baron 
49e9d376f0SJason Baron 
50e9d376f0SJason Baron 
51*ceabef7dSOrson Zhai #if defined(CONFIG_DYNAMIC_DEBUG_CORE)
52a4507fedSRasmus Villemoes int ddebug_add_module(struct _ddebug *tab, unsigned int n,
53a4507fedSRasmus Villemoes 				const char *modname);
54ff49d74aSYehuda Sadeh extern int ddebug_remove_module(const char *mod_name);
55b9075fa9SJoe Perches extern __printf(2, 3)
56906d2015SJoe Perches void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...);
57e9d376f0SJason Baron 
58b48420c1SJim Cromie extern int ddebug_dyndbg_module_param_cb(char *param, char *val,
59b48420c1SJim Cromie 					const char *modname);
60b48420c1SJim Cromie 
61cbc46635SJoe Perches struct device;
62cbc46635SJoe Perches 
63b9075fa9SJoe Perches extern __printf(3, 4)
64906d2015SJoe Perches void __dynamic_dev_dbg(struct _ddebug *descriptor, const struct device *dev,
65b9075fa9SJoe Perches 		       const char *fmt, ...);
66cbc46635SJoe Perches 
67ffa10cb4SJason Baron struct net_device;
68ffa10cb4SJason Baron 
69b9075fa9SJoe Perches extern __printf(3, 4)
70906d2015SJoe Perches void __dynamic_netdev_dbg(struct _ddebug *descriptor,
71ffa10cb4SJason Baron 			  const struct net_device *dev,
72b9075fa9SJoe Perches 			  const char *fmt, ...);
73ffa10cb4SJason Baron 
74923abb9dSGal Pressman struct ib_device;
75923abb9dSGal Pressman 
76923abb9dSGal Pressman extern __printf(3, 4)
77923abb9dSGal Pressman void __dynamic_ibdev_dbg(struct _ddebug *descriptor,
78923abb9dSGal Pressman 			 const struct ib_device *ibdev,
79923abb9dSGal Pressman 			 const char *fmt, ...);
80923abb9dSGal Pressman 
812bdde670SRasmus Villemoes #define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt)		\
82c0d2af63SJoe Perches 	static struct _ddebug  __aligned(8)			\
8307613b0bSJason Baron 	__attribute__((section("__verbose"))) name = {		\
8407613b0bSJason Baron 		.modname = KBUILD_MODNAME,			\
8507613b0bSJason Baron 		.function = __func__,				\
8607613b0bSJason Baron 		.filename = __FILE__,				\
8707613b0bSJason Baron 		.format = (fmt),				\
8807613b0bSJason Baron 		.lineno = __LINE__,				\
8907613b0bSJason Baron 		.flags = _DPRINTK_FLAGS_DEFAULT,		\
902bdde670SRasmus Villemoes 		_DPRINTK_KEY_INIT				\
9107613b0bSJason Baron 	}
9207613b0bSJason Baron 
93e9666d10SMasahiro Yamada #ifdef CONFIG_JUMP_LABEL
949049fc74SJason Baron 
959049fc74SJason Baron #ifdef DEBUG
962bdde670SRasmus Villemoes 
972bdde670SRasmus Villemoes #define _DPRINTK_KEY_INIT .key.dd_key_true = (STATIC_KEY_TRUE_INIT)
989049fc74SJason Baron 
999049fc74SJason Baron #define DYNAMIC_DEBUG_BRANCH(descriptor) \
1009049fc74SJason Baron 	static_branch_likely(&descriptor.key.dd_key_true)
1019049fc74SJason Baron #else
1022bdde670SRasmus Villemoes #define _DPRINTK_KEY_INIT .key.dd_key_false = (STATIC_KEY_FALSE_INIT)
1039049fc74SJason Baron 
1049049fc74SJason Baron #define DYNAMIC_DEBUG_BRANCH(descriptor) \
1059049fc74SJason Baron 	static_branch_unlikely(&descriptor.key.dd_key_false)
1069049fc74SJason Baron #endif
1079049fc74SJason Baron 
1082bdde670SRasmus Villemoes #else /* !HAVE_JUMP_LABEL */
1099049fc74SJason Baron 
1102bdde670SRasmus Villemoes #define _DPRINTK_KEY_INIT
1119049fc74SJason Baron 
1129049fc74SJason Baron #ifdef DEBUG
1139049fc74SJason Baron #define DYNAMIC_DEBUG_BRANCH(descriptor) \
1149049fc74SJason Baron 	likely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
1159049fc74SJason Baron #else
1169049fc74SJason Baron #define DYNAMIC_DEBUG_BRANCH(descriptor) \
1179049fc74SJason Baron 	unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
1189049fc74SJason Baron #endif
1199049fc74SJason Baron 
1209049fc74SJason Baron #endif
1219049fc74SJason Baron 
12247cdd64bSRasmus Villemoes #define __dynamic_func_call(id, fmt, func, ...) do {	\
12347cdd64bSRasmus Villemoes 	DEFINE_DYNAMIC_DEBUG_METADATA(id, fmt);		\
12447cdd64bSRasmus Villemoes 	if (DYNAMIC_DEBUG_BRANCH(id))			\
12547cdd64bSRasmus Villemoes 		func(&id, ##__VA_ARGS__);		\
126e9d376f0SJason Baron } while (0)
127e9d376f0SJason Baron 
12847cdd64bSRasmus Villemoes #define __dynamic_func_call_no_desc(id, fmt, func, ...) do {	\
12947cdd64bSRasmus Villemoes 	DEFINE_DYNAMIC_DEBUG_METADATA(id, fmt);			\
13047cdd64bSRasmus Villemoes 	if (DYNAMIC_DEBUG_BRANCH(id))				\
13147cdd64bSRasmus Villemoes 		func(__VA_ARGS__);				\
13247cdd64bSRasmus Villemoes } while (0)
13347cdd64bSRasmus Villemoes 
13447cdd64bSRasmus Villemoes /*
13547cdd64bSRasmus Villemoes  * "Factory macro" for generating a call to func, guarded by a
13647cdd64bSRasmus Villemoes  * DYNAMIC_DEBUG_BRANCH. The dynamic debug decriptor will be
13747cdd64bSRasmus Villemoes  * initialized using the fmt argument. The function will be called with
13847cdd64bSRasmus Villemoes  * the address of the descriptor as first argument, followed by all
13947cdd64bSRasmus Villemoes  * the varargs. Note that fmt is repeated in invocations of this
14047cdd64bSRasmus Villemoes  * macro.
14147cdd64bSRasmus Villemoes  */
14247cdd64bSRasmus Villemoes #define _dynamic_func_call(fmt, func, ...)				\
14347cdd64bSRasmus Villemoes 	__dynamic_func_call(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__)
14447cdd64bSRasmus Villemoes /*
14547cdd64bSRasmus Villemoes  * A variant that does the same, except that the descriptor is not
14647cdd64bSRasmus Villemoes  * passed as the first argument to the function; it is only called
14747cdd64bSRasmus Villemoes  * with precisely the macro's varargs.
14847cdd64bSRasmus Villemoes  */
14947cdd64bSRasmus Villemoes #define _dynamic_func_call_no_desc(fmt, func, ...)	\
15047cdd64bSRasmus Villemoes 	__dynamic_func_call_no_desc(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__)
15147cdd64bSRasmus Villemoes 
15247cdd64bSRasmus Villemoes #define dynamic_pr_debug(fmt, ...)				\
15347cdd64bSRasmus Villemoes 	_dynamic_func_call(fmt,	__dynamic_pr_debug,		\
15447cdd64bSRasmus Villemoes 			   pr_fmt(fmt), ##__VA_ARGS__)
15547cdd64bSRasmus Villemoes 
15607613b0bSJason Baron #define dynamic_dev_dbg(dev, fmt, ...)				\
15747cdd64bSRasmus Villemoes 	_dynamic_func_call(fmt,__dynamic_dev_dbg, 		\
15847cdd64bSRasmus Villemoes 			   dev, fmt, ##__VA_ARGS__)
159e9d376f0SJason Baron 
16007613b0bSJason Baron #define dynamic_netdev_dbg(dev, fmt, ...)			\
16147cdd64bSRasmus Villemoes 	_dynamic_func_call(fmt, __dynamic_netdev_dbg,		\
16247cdd64bSRasmus Villemoes 			   dev, fmt, ##__VA_ARGS__)
163ffa10cb4SJason Baron 
164923abb9dSGal Pressman #define dynamic_ibdev_dbg(dev, fmt, ...)			\
165923abb9dSGal Pressman 	_dynamic_func_call(fmt, __dynamic_ibdev_dbg,		\
166923abb9dSGal Pressman 			   dev, fmt, ##__VA_ARGS__)
167923abb9dSGal Pressman 
1687a555613SVladimir Kondratiev #define dynamic_hex_dump(prefix_str, prefix_type, rowsize,		\
1697a555613SVladimir Kondratiev 			 groupsize, buf, len, ascii)			\
17047cdd64bSRasmus Villemoes 	_dynamic_func_call_no_desc(__builtin_constant_p(prefix_str) ? prefix_str : "hexdump", \
17147cdd64bSRasmus Villemoes 				   print_hex_dump,			\
17247cdd64bSRasmus Villemoes 				   KERN_DEBUG, prefix_str, prefix_type,	\
17347cdd64bSRasmus Villemoes 				   rowsize, groupsize, buf, len, ascii)
1747a555613SVladimir Kondratiev 
175e9d376f0SJason Baron #else
176e9d376f0SJason Baron 
177b48420c1SJim Cromie #include <linux/string.h>
178b48420c1SJim Cromie #include <linux/errno.h>
179b48420c1SJim Cromie 
180a4507fedSRasmus Villemoes static inline int ddebug_add_module(struct _ddebug *tab, unsigned int n,
181a4507fedSRasmus Villemoes 				    const char *modname)
182a4507fedSRasmus Villemoes {
183a4507fedSRasmus Villemoes 	return 0;
184a4507fedSRasmus Villemoes }
185a4507fedSRasmus Villemoes 
186ff49d74aSYehuda Sadeh static inline int ddebug_remove_module(const char *mod)
187e9d376f0SJason Baron {
188e9d376f0SJason Baron 	return 0;
189e9d376f0SJason Baron }
190e9d376f0SJason Baron 
191b48420c1SJim Cromie static inline int ddebug_dyndbg_module_param_cb(char *param, char *val,
192b48420c1SJim Cromie 						const char *modname)
193b48420c1SJim Cromie {
194b48420c1SJim Cromie 	if (strstr(param, "dyndbg")) {
195516cf1beSJim Cromie 		/* avoid pr_warn(), which wants pr_fmt() fully defined */
196516cf1beSJim Cromie 		printk(KERN_WARNING "dyndbg param is supported only in "
197b48420c1SJim Cromie 			"CONFIG_DYNAMIC_DEBUG builds\n");
198b48420c1SJim Cromie 		return 0; /* allow and ignore */
199b48420c1SJim Cromie 	}
200b48420c1SJim Cromie 	return -EINVAL;
201b48420c1SJim Cromie }
202b48420c1SJim Cromie 
20300b55864SJoe Perches #define dynamic_pr_debug(fmt, ...)					\
20400b55864SJoe Perches 	do { if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); } while (0)
205be70e267SPhilipp Reisner #define dynamic_dev_dbg(dev, fmt, ...)					\
20600b55864SJoe Perches 	do { if (0) dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); } while (0)
207011c7289SArnd Bergmann #define dynamic_hex_dump(prefix_str, prefix_type, rowsize,		\
208011c7289SArnd Bergmann 			 groupsize, buf, len, ascii)			\
209011c7289SArnd Bergmann 	do { if (0)							\
210011c7289SArnd Bergmann 		print_hex_dump(KERN_DEBUG, prefix_str, prefix_type,	\
211011c7289SArnd Bergmann 				rowsize, groupsize, buf, len, ascii);	\
212011c7289SArnd Bergmann 	} while (0)
213e9d376f0SJason Baron #endif
214e9d376f0SJason Baron 
215e9d376f0SJason Baron #endif
216