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

Pau*_*esC 13 linux debugging linux-kernel

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

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;
}
Run Code Online (Sandbox Code Playgroud)

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

而在模块的Makefile文件我已经添加了一行CFLAGS_[id].o := -DDEBUG这里id.c是文件名.

现在我检查/sys/kernel/debug/dynamic_debug/control了这个模块的insmod之后,我在其中找到了下面的行

/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"
Run Code Online (Sandbox Code Playgroud)

即使在完成所有这些之后,令我失望的是,我无法在dmesg的输出中找到上述两个pr_debug语句.那么我错过了什么或做错了什么?

Mil*_*are 12

在Makefile中添加以下内容,假设filename.c是模块源文件.

CFLAGS_filename.o := -DDEBUG
Run Code Online (Sandbox Code Playgroud)

CFLAGS_[filename].o := -DDEBUG
Run Code Online (Sandbox Code Playgroud)

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


Cir*_*四事件 5

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
Run Code Online (Sandbox Code Playgroud)

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

然后我们可以测试一下:

insmod mymodule.ko
Run Code Online (Sandbox Code Playgroud)

它打印了很多额外的调试信息,如下所示:

[   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 
Run Code Online (Sandbox Code Playgroud)

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

[   84.877482]  0xffffffffc0005000 .text            
Run Code Online (Sandbox Code Playgroud)

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

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

echo 8 > /proc/sys/kernel/printk
echo 'module myprintk +p' > /sys/kernel/debug/dynamic_debug/control
insmod /myprintk.ko
Run Code Online (Sandbox Code Playgroud)

这使我们可以pr_debug通过将其添加到我们自己的模块来轻松测试。

使用此设置在内核 4.16 上进行了测试。

printk(KERN_DEBUG!=pr_debug什么时候CONFIG_DYNAMIC_DEBUG=y

这是很不一致,但printk(KERN_DEBUG确实显示了当loglevel=8,即使我们不启用/sys/kernel/debug/dynamic_debug/control,这可以从看出:/sf/answers/26071/​​83021/895245