dynamic debug动态打印- pr_debug

本文详细介绍了Linux内核中Dynamic Debug特性的使用方法,包括如何动态控制内核打印信息,以及在编译时如何使能该特性。适用于需要在运行时调整内核调试输出的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载地址:    https://blog.youkuaiyun.com/u012719256/article/details/55262058

之前介绍过了 printk_once , 只打印一次。

     有些情况下,需要kernel运行时动态打印与否,dynamic debug就派上用场了。

     具体用法可以参考: kernel/Documentationdynamic-debug-howto.txt

     使用步骤:

       //  打印

       echo  -n  "file nand.c  +p" > /sys/kernel/debug/dynamic_debug/control

       // 不打印
       echo  "file nand.c     -p"' > /sys/kernel/debug/dynamic_debug/control

       // 使用例子

       pr_debug("  dynamic debug \n");

 

       奇怪的是,加参数-c, 本地验证是不起作用的。

       至于为什么往 dynamic_debug/control 写入设置信息,可参考内核接口文件代码:

      

[cpp] view plain copy

  1. static int __init dynamic_debug_init(void)  
  2. {  
  3.     struct dentry *dir, *file;  
  4.     struct _ddebug *iter, *iter_start;  
  5.     const char *modname = NULL;  
  6.     int ret = 0;  
  7.     int n = 0;  
  8.   
  9.     dir = debugfs_create_dir("dynamic_debug", NULL);  
  10.     if (!dir)  
  11.         return -ENOMEM;  
  12.     file = debugfs_create_file("control", 0644, dir, NULL,  
  13.                     &ddebug_proc_fops);  
 
  1. static int __init dynamic_debug_init(void)

  2. {

  3. struct dentry *dir, *file;

  4. struct _ddebug *iter, *iter_start;

  5. const char *modname = NULL;

  6. int ret = 0;

  7. int n = 0;

  8.  
  9. dir = debugfs_create_dir("dynamic_debug", NULL);

  10. if (!dir)

  11. return -ENOMEM;

  12. file = debugfs_create_file("control", 0644, dir, NULL,

  13. &ddebug_proc_fops);


 

记得要编译kernel的时候使能,让dynamic debug编译进去。(menuconfig)

  │ Symbol: DYNAMIC_DEBUG [=y]                                                                                                         │ 
  │ Prompt: Enable dynamic printk() support                                                                                            │ 
  │   Defined at lib/Kconfig.debug:1039                                                                                                │ 
  │   Depends on: PRINTK [=y] && DEBUG_FS [=y]                                                                                         │ 
  │   Location:                                                                                                                        │ 
  │     -> Kernel hacking   

   或者直接修改

   ./config 文件
   CONFIG_DYNAMIC_DEBUG=y

   注意的是其依赖于: Depends on: PRINTK [=y] && DEBUG_FS [=y]    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值