Android/Linux驅動開發之使用dev_dbg調試設備驅動

原創作品,轉載時請務必以超鏈接形式標明文章原始出處:http://blog.csdn.net/gqb666/article/details/8789807,作者:gqb666

1、最近在寫I2C下EEPROM的驅動程序,但發現使用i2c_new_probed_device函數無法枚舉到設備,於是想調試該函數(位於driver/i2c/i2c-core.c內),看到其中有些調試信息如下:

  1. i2c_new_probed_device(...)  
  2. {  
  3.     ...  
  4.     if (addr_list[i] == I2C_CLIENT_END) {  
  5.         dev_dbg(&adap->dev, "Probing failed, no device found\n");  
  6.         return NULL;  
  7.     }  
  8.     ...  
  9. }  
但加載驅動模塊,該類調試信息並未打印出來(執行dmesg命令後同樣未找到調試信息)。

2、列出dev_dbg源碼實現:(include/linux/device.h中)

  1. #if defined(DEBUG)  
  2. #define dev_dbg(dev, format, arg...)        \  
  3.     dev_printk(KERN_DEBUG , dev , format , ## arg)  

問題找出,只需在引用頭文件#include/linux/device.h前定義DEBUG宏即可。

在include/linux/i2c.h中修改代碼如下:

  1. #define DEBUG                  /* add for debug eeprom */  
  2. #include <linux/device.h> /* for struct device */  
  3. #undef DEBUG                   /* add for debug eeprom */  

加載驅動驅動模塊後,並沒有調試信息打印出。如下圖:


但執行dmesg命令後Probing failed, no device found已經能夠打印出來,如下圖:


這是爲什麼呢?

3、注意dev_printk的打印級別:

  1. dev_printk(KERN_DEBUG , dev , format , ## arg)  
這裏有個KERN_DEBUG的打印級別,其他級別如下(include/linux/kernel.h中):

  1. #define KERN_EMERG  "<0>" /* system is unusable           */  
  2. #define KERN_ALERT  "<1>" /* action must be taken immediately */  
  3. #define KERN_CRIT   "<2>" /* critical conditions          */  
  4. #define KERN_ERR    "<3>" /* error conditions         */  
  5. #define KERN_WARNING    "<4>" /* warning conditions           */  
  6. #define KERN_NOTICE "<5>" /* normal but significant condition */  
  7. #define KERN_INFO   "<6>" /* informational            */  
  8. #define KERN_DEBUG  "<7>" /* debug-level messages         */  
可以看到KERN_DEBUG級別最低爲7。
再看/kernel/printk.c下的一個宏:

  1. #define DEFAULT_CONSOLE_LOGLEVEL  7 /* anything MORE serious than KERN_DEBUG */  
該行表示只有打印級別高於DEFAULT_CONSOLE_LOGLEVEL(值小於DEFAULT_CONSOLE_LOGLEVEL的值)的打印纔會出現在終端上。而 KERN_DEBUG也爲7,所以我們的調試不會直接打印出來。

將該行修改爲:

  1. #define DEFAULT_CONSOLE_LOGLEVEL  8 /* anything MORE serious than KERN_DEBUG */  
來保證KERN_DEBU的值小於DEFAULT_CONSOLE_LOGLEVEL,然後加載eeprom驅動模塊後:調試信息能夠正確打印出來。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章