原創作品,轉載時請務必以超鏈接形式標明文章原始出處:http://blog.csdn.net/gqb666/article/details/8789807,作者:gqb666
1、最近在寫I2C下EEPROM的驅動程序,但發現使用i2c_new_probed_device函數無法枚舉到設備,於是想調試該函數(位於driver/i2c/i2c-core.c內),看到其中有些調試信息如下:
- i2c_new_probed_device(...)
- {
- ...
- if (addr_list[i] == I2C_CLIENT_END) {
- dev_dbg(&adap->dev, "Probing failed, no device found\n");
- return NULL;
- }
- ...
- }
2、列出dev_dbg源碼實現:(include/linux/device.h中)
- #if defined(DEBUG)
- #define dev_dbg(dev, format, arg...) \
- dev_printk(KERN_DEBUG , dev , format , ## arg)
問題找出,只需在引用頭文件#include/linux/device.h前定義DEBUG宏即可。
在include/linux/i2c.h中修改代碼如下:
- #define DEBUG /* add for debug eeprom */
- #include <linux/device.h> /* for struct device */
- #undef DEBUG /* add for debug eeprom */
加載驅動驅動模塊後,並沒有調試信息打印出。如下圖:
但執行dmesg命令後Probing failed, no device found已經能夠打印出來,如下圖:
這是爲什麼呢?
3、注意dev_printk的打印級別:
- dev_printk(KERN_DEBUG , dev , format , ## arg)
- #define KERN_EMERG "<0>" /* system is unusable */
- #define KERN_ALERT "<1>" /* action must be taken immediately */
- #define KERN_CRIT "<2>" /* critical conditions */
- #define KERN_ERR "<3>" /* error conditions */
- #define KERN_WARNING "<4>" /* warning conditions */
- #define KERN_NOTICE "<5>" /* normal but significant condition */
- #define KERN_INFO "<6>" /* informational */
- #define KERN_DEBUG "<7>" /* debug-level messages */
再看/kernel/printk.c下的一個宏:
- #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
將該行修改爲:
- #define DEFAULT_CONSOLE_LOGLEVEL 8 /* anything MORE serious than KERN_DEBUG */