printk和pr_info之间的区别

Jar*_*vis 17 c kernel-module linux-kernel printk

printkpr_info功能之间的确切区别是什么?在什么条件下,我应该选择一个而不是另一个?

bar*_*lpy 14

内核的printk.h有:

#define pr_info(fmt,arg...) \
    printk(KERN_INFO fmt,##arg)
Run Code Online (Sandbox Code Playgroud)

就像名称一样,pr_info是具有KERN_INFO优先级的printk.


jdk*_*ght 6

当专门查看时pr_info,该定义将依次使用printk(KERN_INFO ...(如barcelona_delpy的答案中所述);但是,答案的源代码片段似乎排除了格式包装器pr_fmt(fmt)(如LPs 注释所述)。


您为什么可以使用pr_infoover 的不同之处在于printk(KERN_INFO ...可以设置的自定义格式。如果您希望使用来为模块中的消息printk添加前缀,则可以在每行上显式添加前缀:

printk(KERN_INFO "mymodule: hello there\n");
// outputs "mymodule: hello there"
Run Code Online (Sandbox Code Playgroud)

要么:

printk(KERN_INFO KBUILD_MODNAME " hello there\n");
// outputs "mymodule: hello there"
Run Code Online (Sandbox Code Playgroud)

但是,如果您使用pr_info(和其他pr_*功能),则可以重新定义格式,pr_info而无需额外工作即可直接使用:

... (includes)
#ifdef pr_fmt
#undef pr_fmt
#endif
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

...
{
    ...
    pr_err("hello there\n");
    // outputs "mymodule: hello there" (assuming module is named 'mymodule')
    ...
}
...
Run Code Online (Sandbox Code Playgroud)

也可以看看: