xref: /linux-6.15/include/linux/dynamic_debug.h (revision c41b20e7)
1 #ifndef _DYNAMIC_DEBUG_H
2 #define _DYNAMIC_DEBUG_H
3 
4 /* dynamic_printk_enabled, and dynamic_printk_enabled2 are bitmasks in which
5  * bit n is set to 1 if any modname hashes into the bucket n, 0 otherwise. They
6  * use independent hash functions, to reduce the chance of false positives.
7  */
8 extern long long dynamic_debug_enabled;
9 extern long long dynamic_debug_enabled2;
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 	char primary_hash;
26 	char secondary_hash;
27 	unsigned int lineno:24;
28 	/*
29  	 * The flags field controls the behaviour at the callsite.
30  	 * The bits here are changed dynamically when the user
31  	 * writes commands to <debugfs>/dynamic_debug/ddebug
32 	 */
33 #define _DPRINTK_FLAGS_PRINT   (1<<0)  /* printk() a message using the format */
34 #define _DPRINTK_FLAGS_DEFAULT 0
35 	unsigned int flags:8;
36 } __attribute__((aligned(8)));
37 
38 
39 int ddebug_add_module(struct _ddebug *tab, unsigned int n,
40 				const char *modname);
41 
42 #if defined(CONFIG_DYNAMIC_DEBUG)
43 extern int ddebug_remove_module(char *mod_name);
44 
45 #define __dynamic_dbg_enabled(dd)  ({	     \
46 	int __ret = 0;							     \
47 	if (unlikely((dynamic_debug_enabled & (1LL << DEBUG_HASH)) &&	     \
48 			(dynamic_debug_enabled2 & (1LL << DEBUG_HASH2))))   \
49 				if (unlikely(dd.flags))			     \
50 					__ret = 1;			     \
51 	__ret; })
52 
53 #define dynamic_pr_debug(fmt, ...) do {					\
54 	static struct _ddebug descriptor				\
55 	__used								\
56 	__attribute__((section("__verbose"), aligned(8))) =		\
57 	{ KBUILD_MODNAME, __func__, __FILE__, fmt, DEBUG_HASH,	\
58 		DEBUG_HASH2, __LINE__, _DPRINTK_FLAGS_DEFAULT };	\
59 	if (__dynamic_dbg_enabled(descriptor))				\
60 		printk(KERN_DEBUG pr_fmt(fmt),	##__VA_ARGS__);		\
61 	} while (0)
62 
63 
64 #define dynamic_dev_dbg(dev, fmt, ...) do {				\
65 	static struct _ddebug descriptor				\
66 	__used								\
67 	__attribute__((section("__verbose"), aligned(8))) =		\
68 	{ KBUILD_MODNAME, __func__, __FILE__, fmt, DEBUG_HASH,	\
69 		DEBUG_HASH2, __LINE__, _DPRINTK_FLAGS_DEFAULT };	\
70 	if (__dynamic_dbg_enabled(descriptor))				\
71 		dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__);	\
72 	} while (0)
73 
74 #else
75 
76 static inline int ddebug_remove_module(char *mod)
77 {
78 	return 0;
79 }
80 
81 #define dynamic_pr_debug(fmt, ...)					\
82 	do { if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); } while (0)
83 #define dynamic_dev_dbg(dev, format, ...)				\
84 	do { if (0) dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); } while (0)
85 #endif
86 
87 #endif
88