问题描述
我写了下面提到的小的内核模块代码,我正在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\n ");
return 0;
}
void cleanup_mod_func(void)
{
printk(KERN_INFO "My module removed\n ");
}
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中\n
之后的space
引起的.
但是我不明白为什么会这样.
With closure look, I found out that it is happening because of space
after \n
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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!