如何输出dev_dbg log

本文介绍了一种在Linux内核中利用动态调试功能输出特定模块日志的方法,通过修改debugfs中的控制文件,可以灵活地控制dev_dbg等宏的输出,避免频繁修改源代码和重新编译。
<div id="article_content" class="article_content">


<span style="font-family:'lucida Grande',Verdana; font-size:14px; line-height:23px">&nbsp; &nbsp; kernel里面默认都配置了CONFIG_DYNAMIC_DEBUG=y;</span><br style="font-family:'lucida Grande',Verdana; font-size:14px; line-height:23px">
<span style="font-family:'lucida Grande',Verdana; font-size:14px; line-height:23px">&nbsp;&nbsp; &nbsp; 假如我们要打印acpuclock-krait.c中的</span><br style="font-family:'lucida Grande',Verdana; font-size:14px; line-height:23px">
<span style="font-family:'lucida Grande',Verdana; font-size:14px; line-height:23px">&nbsp;&nbsp; &nbsp; dev_dbg(drv.dev, "ACPU%d speed change complete\n", cpu);</span><br style="font-family:'lucida Grande',Verdana; font-size:14px; line-height:23px">
<span style="font-family:'lucida Grande',Verdana; font-size:14px; line-height:23px">&nbsp;&nbsp; &nbsp; 在adb shell中输入:</span><br style="font-family:'lucida Grande',Verdana; font-size:14px; line-height:23px">
<span style="font-family:'lucida Grande',Verdana; font-size:14px; line-height:23px">&nbsp;&nbsp; &nbsp; mount –t debugfs none /sys/kernel/debug&nbsp;</span><br style="font-family:'lucida Grande',Verdana; font-size:14px; line-height:23px">
<span style="font-family:'lucida Grande',Verdana; font-size:14px; line-height:23px">&nbsp;&nbsp; &nbsp; cd /sys/kernel/debug&nbsp;</span><br style="font-family:'lucida Grande',Verdana; font-size:14px; line-height:23px">
<span style="font-family:'lucida Grande',Verdana; font-size:14px; line-height:23px">&nbsp;&nbsp; &nbsp; echo -n 'file acpuclock-krait.c +p' &gt; dynamic_debug/control</span><br style="font-family:'lucida Grande',Verdana; font-size:14px; line-height:23px">
<p><span style="font-family:'lucida Grande',Verdana; font-size:14px; line-height:23px">&nbsp;&nbsp; &nbsp; 上面是大家熟悉的debugfs;下面是打印出的log:</span></p>
<p><span style="font-family:'lucida Grande',Verdana; font-size:14px; line-height:23px"><img src="https://img-blog.youkuaiyun.com/20130525222623659" alt=""><br>
</span></p>
<br style="font-family:'lucida Grande',Verdana; font-size:14px; line-height:23px">
<span style="font-family:'lucida Grande',Verdana; font-size:14px; line-height:23px">&nbsp; &nbsp; 个人觉得懂得这个方法输出log很重要,linux里面许许多多的地方使用了dev_dbg,总不能每次想输出log改成dev_err,再build fastboot,</span><br style="font-family:'lucida Grande',Verdana; font-size:14px; line-height:23px">
<span style="font-family:'lucida Grande',Verdana; font-size:14px; line-height:23px">为什么这样就可以输出Log了?请参考网上文章:</span><br style="font-family:'lucida Grande',Verdana; font-size:14px; line-height:23px">
<a href="http://blog.youkuaiyun.com/pillarbuaa/article/details/7634546" target="_blank" style="outline:none; color:rgb(61,94,134); font-family:'lucida Grande',Verdana; font-size:14px; line-height:23px">http://blog.<wbr>youkuaiyun.com/pil<wbr>larbuaa/arti<wbr>cle/details/<wbr>7634546</a><br style="font-family:'lucida Grande',Verdana; font-size:14px; line-height:23px">
   
</div>
<think>嗯,用户提供了三条引用信息,但核心问题是解决AHCISATA_DBG_LOG打印不显示的问题。结合Linux内核调试经验,这显然属于内核动态打印(dynamicdebug)的配置问题。用户应该是嵌入式开发或内核驱动工程师,正在调试SATA控制器相关的驱动。从ta直接抛出宏名称AHCISATA_DBG_LOG来看,ta对内核代码有一定了解,可能正在阅读AHCI驱动源码(比如drivers/ata/ahci.c),但不确定如何激活调试输出。参考用户引用的关键信息:[^1]说明print_hex_dump_debug()的依赖关系,[^2]展示了dynamicdebug的经典用法。需要特别注意第二点引用的控制语法,这直接提供了解决方案模板。第三点虽然提到内存检测算法,但与当前问题无关。核心解决思路应该是:1.确认内核编译时必须启用CONFIG_DYNAMIC_DEBUG,这是基础前提2.用户驱动代码里一定有类似pr_debug(AHCISATA_DBG_LOG)这样的调用,重点要定位具体文件名和函数名3.通过dynamicdebug控制接口激活对应位置的打印用户可能遇到的典型情况:-内核未启用DYNAMIC_DEBUG(需重新编译)-控制命令路径写错(sysfs路径严格区分内核版本)-驱动调试宏名称与源文件不匹配(建议用grep在drivers/ata目录查找)考虑到用户正在调试,补充了两个进阶方案:1.模块参数方案适合需要长期调试的场景2.debugfs方案更适合临时查看最后生成的问题覆盖了常见延伸需求:1.性能影响是工程师必然关心的2.模块参数用法是进阶调试技巧3.printk对照能帮助理解调试分级特别提醒了三次检查拼写,因为dynamicdebug路径稍有偏差就会失效,这是新手最容易踩的坑。</think> 要解决Linux内核中`AHCISATA_DBG_LOG`宏的调试输出不显示问题,需结合动态打印调试(Dynamic Debug)机制进行配置。以下是完整解决方案: ### 核心解决方案 1. **确认内核配置** 检查内核是否启用动态调试支持: ```bash grep CONFIG_DYNAMIC_DEBUG /boot/config-$(uname -r) ``` *若输出 `CONFIG_DYNAMIC_DEBUG=y` 表示支持,否则需重新编译内核*[^1] 2. **激活AHCI驱动的动态调试** 根据`AHCISATA_DBG_LOG`实现代码(通常在`drivers/ata/ahci.c`)定位调试宏,执行: ```bash # 激活整个AHCI驱动文件的调试 echo "file drivers/ata/ahci.c +p" > /sys/kernel/debug/dynamic_debug/control # 若需精确到函数(如ahci_init_one) echo "func ahci_init_one +p" > /sys/kernel/debug/dynamic_debug/control ``` **注**:`+p`表示启用打印,`-p`表示禁用[^2] 3. **检查宏实现逻辑** 在源码中确认宏定义(典型实现): ```c #define AHCISATA_DBG_LOG(fmt, args...) \ pr_debug("%s: " fmt, __func__, ## args) ``` *必须通过`pr_debug()`或`dev_dbg()`输出才能被dynamic debug捕获*[^1] ### 备选方案 如果仍无效: 1. **强制开启所有调试**(临时方案) ```bash echo -n "module ahci +p" > /sys/kernel/debug/dynamic_debug/control ``` *可能产生大量日志,仅建议测试* 2. **检查Dmesg过滤器** ```bash # 查看当前日志级别 cat /proc/sys/kernel/printk # 设置KERN_DEBUG级别可见(临时生效) echo "7 4 1 7" > /proc/sys/kernel/printk ``` ### 结果验证 ```bash # 监控内核日志 dmesg -wH ``` 触发SATA设备操作(如插拔硬盘),观察是否输出`AHCISATA_DBG_LOG`的调试信息。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值