內核中的大部分驅動都使用了dev_dbg接口打印調試信息,默認是不會輸出到控制檯的。
先看一下dev_dbg的定義:
文件路徑:/kernel/include/linux/device.h
#if defined(CONFIG_DYNAMIC_DEBUG)
#define dev_dbg(dev, format, ...) \
do { \
dynamic_dev_dbg(dev, format, ##__VA_ARGS__); \
} while (0)
#elif defined(DEBUG)
#define dev_dbg(dev, format, arg...) \
dev_printk(KERN_DEBUG, dev, format, ##arg)
#else
#define dev_dbg(dev, format, arg...) \
({ \
if (0) \
dev_printk(KERN_DEBUG, dev, format, ##arg); \
})
#endif
動態調試dev_dbg要打開CONFIG_DYNAMIC_DEBUG這個配置項,具體沒有操作過,其中的原理也沒有跟蹤分析。這裏指介紹第二種開啓dev_dbg的方式。
在需要打印dev_dbg調試信息的驅動文件開頭定義DEBUG宏。注意必須是在<linux/device.h>前面:
#define DEBUG
#include <linux/device.h>
打開DEBUG宏是第一步,這個時候還是不能輸出到控制檯的,還必須要修改printk打印等級。
printk打印等級:
#define KERN_EMERG KERN_SOH "0" /* system is unusable */
#define KERN_ALERT KERN_SOH "1" /* action must be taken immediately */
#define KERN_CRIT KERN_SOH "2" /* critical conditions */
#define KERN_ERR KERN_SOH "3" /* error conditions */
#define KERN_WARNING KERN_SOH "4" /* warning conditions */
#define KERN_NOTICE KERN_SOH "5" /* normal but significant condition */
#define KERN_INFO KERN_SOH "6" /* informational */
#define KERN_DEBUG KERN_SOH "7" /* debug-level messages */
printk.c中定義的默認打印等級如下:
#define CONSOLE_LOGLEVEL_SILENT 0 /* Mum's the word */
#define CONSOLE_LOGLEVEL_MIN 1 /* Minimum loglevel we let people use */
#define CONSOLE_LOGLEVEL_QUIET 4 /* Shhh ..., when booted with "quiet" */
#define CONSOLE_LOGLEVEL_DEFAULT 7 /* anything MORE serious than KERN_DEBUG */
printk默認的打印等級是7,而dev_dbg的打印等級也是7,只有等級高於printk默認打印等級的日誌信息才能輸出出來。
所以這裏直接修改DEFAULT_CONSOLE_LOGLEVEL爲8,這樣dev_dbg就可以輸出了,當然還有其他的修改方式。
在cmdline中傳入loglevel=8也能輸出dev_dbg日誌。