为什么Linux内核的pr_debug没有输出?

18

我有一个可加载内核模块,它的初始化如下所示

static int __init id_init(void)
{
    struct identity *temp;

    /* some code which is not relevant to the question */

    temp = identity_find(3);
    pr_debug("id 3 = %s\n", temp->name);

    temp = identity_find(42);
    if (temp == NULL)
        pr_debug("id 42 not found\n");

    /* some code which is not relevant to the question */

    return 0;
}

我在使用的内核版本上启用了动态调试 - 即CONFIG_DYNAMIC_DEBUG=y

在模块的Makefile中,我添加了一行CFLAGS_[id].o := -DDEBUG,其中id.c是文件名。

现在,我在此模块进行insmod后检查了/sys/kernel/debug/dynamic_debug/control,在其中找到以下行:

/home/pauldc/Programming/Kernel/id/id.c:69 [id]id_init =_ "id 42 not found\012"
/home/pauldc/Programming/Kernel/id/id.c:65 [id]id_init =_ "id 3 = %s\012"
即使我做了这一切,令我失望的是,在dmesg输出中我仍然找不到上述两个pr_debug语句。那么我错过了什么或者做错了什么呢?

检查您系统的日志级别。也许不应该打印调试日志。 - Milind Dumbare
1
你在dmesg或屏幕上没有看到吗?试着在加载模块时添加dyndbg参数。如果您已经编译,请使用<NAME>.dyndbg ,其中<NAME>是您的模块名称,根据Makefile相应地使用。 - 0andriy
@Miline 你是不是指的 CONFIG_MESSAGE_LOGLEVEL_DEFAULT?它是4,就如配置文件所报告的。但这会有什么不同吗? - PaulDaviesC
你为什么不能使用printk? - Milind Dumbare
@AndyShevchenko,我没明白。你能详细解释一下吗? - PaulDaviesC
显示剩余3条评论
2个回答

19

或者,要为整个模块启用调试,您可以修改该目录中的Makefile并将标志添加到ccflags-y中,如果我正确理解了本文档的第4.2节:https://www.kernel.org/doc/Documentation/kbuild/modules.txt - clearlight

9

CONFIG_DYNAMIC_DEBUG=y

https://www.kernel.org/doc/html/v4.11/admin-guide/dynamic-debug-howto.html

如果使用此选项编译内核,您可以做出惊人的事情:

echo 8 > /proc/sys/kernel/printk
echo 'file kernel/module.c +p' > /sys/kernel/debug/dynamic_debug/control

这将有选择地启用您想要的pr_debug()

然后,我们可以使用以下方法进行测试:

insmod mymodule.ko

这将打印大量额外的调试信息,例如:

[   84.875592] init_module: umod=0000000073518b66, len=185416, uargs=000000009c6e375a                    
[   84.876099] Core section allocation order:       
[   84.876257]  .text                               
[   84.876332]  .note.gnu.build-id                  
[   84.876418]  .rodata.str1.1                      
[   84.876492]  .orc_unwind_ip                      
[   84.876568]  .orc_unwind                         
[   84.876636]  __mcount_loc                        
[   84.876705]  .data                               
[   84.876760]  .gnu.linkonce.this_module           
[   84.876856]  .bss                                
[   84.876919] Init section allocation order:       
[   84.877041]  .symtab                             
[   84.877121]  .strtab                             
[   84.877235] final section addresses:             
[   84.877352]  0xffffffffc0006000 .note.gnu.build-id                                                    
[   84.877482]  0xffffffffc0005000 .text            
[   84.877580]  0xffffffffc0006024 .rodata.str1.1   
[   84.877695]  0xffffffffc0006040 .orc_unwind_ip   
[   84.877805]  0xffffffffc0006050 .orc_unwind      
[   84.877905]  0xffffffffc0006068 __mcount_loc     
[   84.878012]  0xffffffffc0007000 .data            
[   84.878107]  0xffffffffc0007000 .gnu.linkonce.this_module                                             
[   84.878238]  0xffffffffc0007340 .bss             
[   84.878331]  0xffffffffc000a000 .symtab          
[   84.878430]  0xffffffffc000a348 .strtab          
[   84.878657] Absolute symbol: 0x00000000          
[   84.878951] Absolute symbol: 0x00000000          
[   84.879713] hello init 

特别地,它包含了模块加载地址

[   84.877482]  0xffffffffc0005000 .text            

这对于将地址转换为行非常有用。

对于模块,我们可以这样做:

echo 8 > /proc/sys/kernel/printk
echo 'module myprintk +p' > /sys/kernel/debug/dynamic_debug/control
insmod /myprintk.ko

这使我们能够通过将pr_debug添加到我们自己的模块中,轻松地测试它。

在使用此设置测试了内核版本为4.16。

printk(KERN_DEBUGCONFIG_DYNAMIC_DEBUG=y不同时等于pr_debug

这很不一致,但是,当loglevel=8时,即使我们没有启用/sys/kernel/debug/dynamic_debug/controlprintk(KERN_DEBUG也会显示出来,可以从以下链接中看到:https://dev59.com/ZZffa4cB1Zd3GeqP3SGp#37283021


默认日志级别是从哪里来的,如果没有启用动态,它们会在哪里显示?您能否挖掘一些剩余的日志消息(低于默认日志级别的消息),或者它们根本不会被发出? - sherrellbc
@sherrellbc 我现在有点懒得去研究,但是通过一些快速实验/查看源代码应该很容易找到答案,如果你找到了请告诉我。 - Ciro Santilli OurBigBook.com

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接