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内),看到其中有些调试信息如下:

[cpp]  view plain copy
  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中)

[cpp]  view plain copy
  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中修改代码如下:

[cpp]  view plain copy
  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的打印级别:

[cpp]  view plain copy
  1. dev_printk(KERN_DEBUG , dev , format , ## arg)  
这里有个KERN_DEBUG的打印级别,其他级别如下(include/linux/kernel.h中):

[cpp]  view plain copy
  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下的一个宏:

[cpp]  view plain copy
  1. #define DEFAULT_CONSOLE_LOGLEVEL  7 /* anything MORE serious than KERN_DEBUG */  
该行表示只有打印级别高于DEFAULT_CONSOLE_LOGLEVEL(值小于DEFAULT_CONSOLE_LOGLEVEL的值)的打印才会出现在终端上。而 KERN_DEBUG也为7,所以我们的调试不会直接打印出来。

将该行修改为:

[cpp]  view plain copy
  1. #define DEFAULT_CONSOLE_LOGLEVEL  8 /* anything MORE serious than KERN_DEBUG */  
来保证KERN_DEBU的值小于DEFAULT_CONSOLE_LOGLEVEL,然后加载eeprom驱动模块后:调试信息能够正确打印出来。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值