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

Why is pr_debug of the Linux kernel not giving any output?

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

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 found2"
/home/pauldc/Programming/Kernel/id/id.c:65 [id]id_init =_ "id 3 = %s2"

即使做了所有这些,令我失望的是我在 dmesg 的输出中找不到以上两个 pr_debug 语句。那么我错过了什么或做错了什么?

将以下内容添加到 Makefile,假设 filename.c 是模块源文件。

CFLAGS_filename.o := -DDEBUG

没有

CFLAGS_[filename].o := -DDEBUG

参考https://www.kernel.org/doc/local/pr_debug.txt

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 

特别是,它包含 module load address:

[   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 上测试 this setup

printk(KERN_DEBUG != pr_debugCONFIG_DYNAMIC_DEBUG=y

这很不一致,但是printk(KERN_DEBUG确实在loglevel=8时出现,即使我们不启用/sys/kernel/debug/dynamic_debug/control,这可以从: