为什么printk不在内核日志(dmesg)中打印消息 [英] Why printk doesn't print message in kernel log(dmesg)

查看:48
本文介绍了为什么printk不在内核日志(dmesg)中打印消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了下面提到的小内核模块代码,我正在 ubuntu 14.04

I wrote small kernel module code as mentioned below, I am testing it in ubuntu 14.04

#include <linux/module.h> 
#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/init.h>

int init_mod_func(void)
{
    printk(KERN_INFO "My module inserted
 ");
    return 0;
}

void cleanup_mod_func(void)
{
    printk(KERN_INFO "My module removed
 ");
}

module_init(init_mod_func);
module_exit(cleanup_mod_func);


MODULE_AUTHOR("Ankur");
MODULE_DESCRIPTION("TEST MODULE");
MODULE_LICENSE("GPL");

现在,当我使用 insmod 编译并插入上面的模块时,我在 dmesg 中看不到 printk 消息.但是在使用 rmmod 删除模块后,我看到了两个 printk 消息.

Now when I compile and insert above module using insmod I don't see printk message in the dmesg. However after module removal using rmmod I see both the printk messages.

通过闭包查看,我发现这是因为 printk 中 之后的 space 造成的.
但是我不明白为什么会这样.

With closure look, I found out that it is happening because of space after in the printk.
However I don't get why it is like that.

ankur:~/temp/tmp$ 
ankur:~/temp/tmp$ 
ankur:~/temp/tmp$ sudo dmesg -C /dev/null
ankur:~/temp/tmp$ 
ankur:~/temp/tmp$ 
ankur:~/temp/tmp$ sudo insmod testmod.ko 
ankur:~/temp/tmp$ dmesg
ankur:~/temp/tmp$ 
ankur:~/temp/tmp$ sudo rmmod testmod
ankur:~/temp/tmp$ dmesg
[ 4062.140441] My module inserted
[ 4062.140441]  
[ 4073.324994] My module removed
[ 4073.324994]

推荐答案

内核日志环形缓冲区的行为就好像它是行缓冲的,正如在实现中可以看到的:

The kernel log ring buffer behaves as if it were line-buffered, as it can be seen in the implementation:

  • vprintk_emit 您可以看到它如何根据尾随换行符的存在将缓冲区标记为刷新/缓冲

  • in vprintk_emit you can see how it marks a buffer to be flushed/buffered based on the existence of a trailing newline

log_output 您可以看到负责刷新或缓冲消息的实际代码

in log_output you can see the actual code which takes care of flushing or buffering the message

很明显,您的消息不包含尾随换行符.如果您删除了尾随空间,您将在 printk 调用中刷新后在内核日志中看到消息.

As it is evident, your messages do not contain trailing newlines. Had you deleted the trailing space, you would see the messages in the kernel log after they had been flushed in the printk call(s).

这篇关于为什么printk不在内核日志(dmesg)中打印消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆