xref: /linux-6.15/include/linux/dynamic_debug.h (revision b7b4eebd)
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)
35640d1eafSJim Cromie 
36640d1eafSJim Cromie #define _DPRINTK_FLAGS_INCL_ANY		\
37640d1eafSJim Cromie 	(_DPRINTK_FLAGS_INCL_MODNAME | _DPRINTK_FLAGS_INCL_FUNCNAME |\
38640d1eafSJim Cromie 	 _DPRINTK_FLAGS_INCL_LINENO  | _DPRINTK_FLAGS_INCL_TID)
39640d1eafSJim Cromie 
40b558c96fSJim Cromie #if defined DEBUG
41b558c96fSJim Cromie #define _DPRINTK_FLAGS_DEFAULT _DPRINTK_FLAGS_PRINT
42b558c96fSJim Cromie #else
43e9d376f0SJason Baron #define _DPRINTK_FLAGS_DEFAULT 0
44b558c96fSJim Cromie #endif
45e9d376f0SJason Baron 	unsigned int flags:8;
46e9666d10SMasahiro Yamada #ifdef CONFIG_JUMP_LABEL
479049fc74SJason Baron 	union {
489049fc74SJason Baron 		struct static_key_true dd_key_true;
499049fc74SJason Baron 		struct static_key_false dd_key_false;
509049fc74SJason Baron 	} key;
519049fc74SJason Baron #endif
52e9d376f0SJason Baron } __attribute__((aligned(8)));
53e9d376f0SJason Baron 
54*b7b4eebdSJim Cromie /* encapsulate linker provided built-in (or module) dyndbg data */
55*b7b4eebdSJim Cromie struct _ddebug_info {
56*b7b4eebdSJim Cromie 	struct _ddebug *descs;
57*b7b4eebdSJim Cromie 	unsigned int num_descs;
58*b7b4eebdSJim Cromie };
59e9d376f0SJason Baron 
60ceabef7dSOrson Zhai #if defined(CONFIG_DYNAMIC_DEBUG_CORE)
61a2d375edSJim Cromie 
62*b7b4eebdSJim Cromie int ddebug_add_module(struct _ddebug_info *dyndbg, const char *modname);
63*b7b4eebdSJim Cromie 
64ff49d74aSYehuda Sadeh extern int ddebug_remove_module(const char *mod_name);
65b9075fa9SJoe Perches extern __printf(2, 3)
66906d2015SJoe Perches void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...);
67e9d376f0SJason Baron 
68b48420c1SJim Cromie extern int ddebug_dyndbg_module_param_cb(char *param, char *val,
69b48420c1SJim Cromie 					const char *modname);
70b48420c1SJim Cromie 
71cbc46635SJoe Perches struct device;
72cbc46635SJoe Perches 
73b9075fa9SJoe Perches extern __printf(3, 4)
74906d2015SJoe Perches void __dynamic_dev_dbg(struct _ddebug *descriptor, const struct device *dev,
75b9075fa9SJoe Perches 		       const char *fmt, ...);
76cbc46635SJoe Perches 
77ffa10cb4SJason Baron struct net_device;
78ffa10cb4SJason Baron 
79b9075fa9SJoe Perches extern __printf(3, 4)
80906d2015SJoe Perches void __dynamic_netdev_dbg(struct _ddebug *descriptor,
81ffa10cb4SJason Baron 			  const struct net_device *dev,
82b9075fa9SJoe Perches 			  const char *fmt, ...);
83ffa10cb4SJason Baron 
84923abb9dSGal Pressman struct ib_device;
85923abb9dSGal Pressman 
86923abb9dSGal Pressman extern __printf(3, 4)
87923abb9dSGal Pressman void __dynamic_ibdev_dbg(struct _ddebug *descriptor,
88923abb9dSGal Pressman 			 const struct ib_device *ibdev,
89923abb9dSGal Pressman 			 const char *fmt, ...);
90923abb9dSGal Pressman 
912bdde670SRasmus Villemoes #define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt)		\
92c0d2af63SJoe Perches 	static struct _ddebug  __aligned(8)			\
9333def849SJoe Perches 	__section("__dyndbg") name = {				\
9407613b0bSJason Baron 		.modname = KBUILD_MODNAME,			\
9507613b0bSJason Baron 		.function = __func__,				\
9607613b0bSJason Baron 		.filename = __FILE__,				\
9707613b0bSJason Baron 		.format = (fmt),				\
9807613b0bSJason Baron 		.lineno = __LINE__,				\
9907613b0bSJason Baron 		.flags = _DPRINTK_FLAGS_DEFAULT,		\
1002bdde670SRasmus Villemoes 		_DPRINTK_KEY_INIT				\
10107613b0bSJason Baron 	}
10207613b0bSJason Baron 
103e9666d10SMasahiro Yamada #ifdef CONFIG_JUMP_LABEL
1049049fc74SJason Baron 
1059049fc74SJason Baron #ifdef DEBUG
1062bdde670SRasmus Villemoes 
1072bdde670SRasmus Villemoes #define _DPRINTK_KEY_INIT .key.dd_key_true = (STATIC_KEY_TRUE_INIT)
1089049fc74SJason Baron 
1099049fc74SJason Baron #define DYNAMIC_DEBUG_BRANCH(descriptor) \
1109049fc74SJason Baron 	static_branch_likely(&descriptor.key.dd_key_true)
1119049fc74SJason Baron #else
1122bdde670SRasmus Villemoes #define _DPRINTK_KEY_INIT .key.dd_key_false = (STATIC_KEY_FALSE_INIT)
1139049fc74SJason Baron 
1149049fc74SJason Baron #define DYNAMIC_DEBUG_BRANCH(descriptor) \
1159049fc74SJason Baron 	static_branch_unlikely(&descriptor.key.dd_key_false)
1169049fc74SJason Baron #endif
1179049fc74SJason Baron 
118a2d375edSJim Cromie #else /* !CONFIG_JUMP_LABEL */
1199049fc74SJason Baron 
1202bdde670SRasmus Villemoes #define _DPRINTK_KEY_INIT
1219049fc74SJason Baron 
1229049fc74SJason Baron #ifdef DEBUG
1239049fc74SJason Baron #define DYNAMIC_DEBUG_BRANCH(descriptor) \
1249049fc74SJason Baron 	likely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
1259049fc74SJason Baron #else
1269049fc74SJason Baron #define DYNAMIC_DEBUG_BRANCH(descriptor) \
1279049fc74SJason Baron 	unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT)
1289049fc74SJason Baron #endif
1299049fc74SJason Baron 
130a2d375edSJim Cromie #endif /* CONFIG_JUMP_LABEL */
1319049fc74SJason Baron 
13247cdd64bSRasmus Villemoes #define __dynamic_func_call(id, fmt, func, ...) do {	\
13347cdd64bSRasmus Villemoes 	DEFINE_DYNAMIC_DEBUG_METADATA(id, fmt);		\
13447cdd64bSRasmus Villemoes 	if (DYNAMIC_DEBUG_BRANCH(id))			\
13547cdd64bSRasmus Villemoes 		func(&id, ##__VA_ARGS__);		\
136e9d376f0SJason Baron } while (0)
137e9d376f0SJason Baron 
13847cdd64bSRasmus Villemoes #define __dynamic_func_call_no_desc(id, fmt, func, ...) do {	\
13947cdd64bSRasmus Villemoes 	DEFINE_DYNAMIC_DEBUG_METADATA(id, fmt);			\
14047cdd64bSRasmus Villemoes 	if (DYNAMIC_DEBUG_BRANCH(id))				\
14147cdd64bSRasmus Villemoes 		func(__VA_ARGS__);				\
14247cdd64bSRasmus Villemoes } while (0)
14347cdd64bSRasmus Villemoes 
14447cdd64bSRasmus Villemoes /*
14547cdd64bSRasmus Villemoes  * "Factory macro" for generating a call to func, guarded by a
146e5ebffe1SJim Cromie  * DYNAMIC_DEBUG_BRANCH. The dynamic debug descriptor will be
14747cdd64bSRasmus Villemoes  * initialized using the fmt argument. The function will be called with
14847cdd64bSRasmus Villemoes  * the address of the descriptor as first argument, followed by all
14947cdd64bSRasmus Villemoes  * the varargs. Note that fmt is repeated in invocations of this
15047cdd64bSRasmus Villemoes  * macro.
15147cdd64bSRasmus Villemoes  */
15247cdd64bSRasmus Villemoes #define _dynamic_func_call(fmt, func, ...)				\
15347cdd64bSRasmus Villemoes 	__dynamic_func_call(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__)
15447cdd64bSRasmus Villemoes /*
15547cdd64bSRasmus Villemoes  * A variant that does the same, except that the descriptor is not
15647cdd64bSRasmus Villemoes  * passed as the first argument to the function; it is only called
15747cdd64bSRasmus Villemoes  * with precisely the macro's varargs.
15847cdd64bSRasmus Villemoes  */
15947cdd64bSRasmus Villemoes #define _dynamic_func_call_no_desc(fmt, func, ...)	\
16047cdd64bSRasmus Villemoes 	__dynamic_func_call_no_desc(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__)
16147cdd64bSRasmus Villemoes 
16247cdd64bSRasmus Villemoes #define dynamic_pr_debug(fmt, ...)				\
16347cdd64bSRasmus Villemoes 	_dynamic_func_call(fmt,	__dynamic_pr_debug,		\
16447cdd64bSRasmus Villemoes 			   pr_fmt(fmt), ##__VA_ARGS__)
16547cdd64bSRasmus Villemoes 
16607613b0bSJason Baron #define dynamic_dev_dbg(dev, fmt, ...)				\
16747cdd64bSRasmus Villemoes 	_dynamic_func_call(fmt,__dynamic_dev_dbg, 		\
16847cdd64bSRasmus Villemoes 			   dev, fmt, ##__VA_ARGS__)
169e9d376f0SJason Baron 
17007613b0bSJason Baron #define dynamic_netdev_dbg(dev, fmt, ...)			\
17147cdd64bSRasmus Villemoes 	_dynamic_func_call(fmt, __dynamic_netdev_dbg,		\
17247cdd64bSRasmus Villemoes 			   dev, fmt, ##__VA_ARGS__)
173ffa10cb4SJason Baron 
174923abb9dSGal Pressman #define dynamic_ibdev_dbg(dev, fmt, ...)			\
175923abb9dSGal Pressman 	_dynamic_func_call(fmt, __dynamic_ibdev_dbg,		\
176923abb9dSGal Pressman 			   dev, fmt, ##__VA_ARGS__)
177923abb9dSGal Pressman 
1787a555613SVladimir Kondratiev #define dynamic_hex_dump(prefix_str, prefix_type, rowsize,		\
1797a555613SVladimir Kondratiev 			 groupsize, buf, len, ascii)			\
18047cdd64bSRasmus Villemoes 	_dynamic_func_call_no_desc(__builtin_constant_p(prefix_str) ? prefix_str : "hexdump", \
18147cdd64bSRasmus Villemoes 				   print_hex_dump,			\
18247cdd64bSRasmus Villemoes 				   KERN_DEBUG, prefix_str, prefix_type,	\
18347cdd64bSRasmus Villemoes 				   rowsize, groupsize, buf, len, ascii)
1847a555613SVladimir Kondratiev 
185a2d375edSJim Cromie #else /* !CONFIG_DYNAMIC_DEBUG_CORE */
186e9d376f0SJason Baron 
187b48420c1SJim Cromie #include <linux/string.h>
188b48420c1SJim Cromie #include <linux/errno.h>
189a2d375edSJim Cromie #include <linux/printk.h>
190b48420c1SJim Cromie 
191*b7b4eebdSJim Cromie static inline int ddebug_add_module(struct _ddebug_info *dinfo, const char *modname)
192a4507fedSRasmus Villemoes {
193a4507fedSRasmus Villemoes 	return 0;
194a4507fedSRasmus Villemoes }
195a4507fedSRasmus Villemoes 
196ff49d74aSYehuda Sadeh static inline int ddebug_remove_module(const char *mod)
197e9d376f0SJason Baron {
198e9d376f0SJason Baron 	return 0;
199e9d376f0SJason Baron }
200e9d376f0SJason Baron 
201b48420c1SJim Cromie static inline int ddebug_dyndbg_module_param_cb(char *param, char *val,
202b48420c1SJim Cromie 						const char *modname)
203b48420c1SJim Cromie {
20485d6b66dSJim Cromie 	if (!strcmp(param, "dyndbg")) {
205516cf1beSJim Cromie 		/* avoid pr_warn(), which wants pr_fmt() fully defined */
206516cf1beSJim Cromie 		printk(KERN_WARNING "dyndbg param is supported only in "
207b48420c1SJim Cromie 			"CONFIG_DYNAMIC_DEBUG builds\n");
208b48420c1SJim Cromie 		return 0; /* allow and ignore */
209b48420c1SJim Cromie 	}
210b48420c1SJim Cromie 	return -EINVAL;
211b48420c1SJim Cromie }
212b48420c1SJim Cromie 
21300b55864SJoe Perches #define dynamic_pr_debug(fmt, ...)					\
21400b55864SJoe Perches 	do { if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); } while (0)
215be70e267SPhilipp Reisner #define dynamic_dev_dbg(dev, fmt, ...)					\
21600b55864SJoe Perches 	do { if (0) dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); } while (0)
217011c7289SArnd Bergmann #define dynamic_hex_dump(prefix_str, prefix_type, rowsize,		\
218011c7289SArnd Bergmann 			 groupsize, buf, len, ascii)			\
219011c7289SArnd Bergmann 	do { if (0)							\
220011c7289SArnd Bergmann 		print_hex_dump(KERN_DEBUG, prefix_str, prefix_type,	\
221011c7289SArnd Bergmann 				rowsize, groupsize, buf, len, ascii);	\
222011c7289SArnd Bergmann 	} while (0)
223a2d375edSJim Cromie 
224a2d375edSJim Cromie #endif /* !CONFIG_DYNAMIC_DEBUG_CORE */
225e9d376f0SJason Baron 
226e9d376f0SJason Baron #endif
227