为什么我的内核日志没有显示最新的输出?

问题描述:

我正在linux ubuntu 17.04中编写一个简单的内核模块,该模块需要一个字符串并将其打印在内核日志中.

I'm coding a simple kernel module, in linux ubuntu 17.04, that takes a string and prints it in the kernel log.

#include<linux/module.h>
#include<linux/init.h>
#include<linux/moduleparam.h>
char* mystring = "hello world";
module_param(mystring ,charp ,S_IRUSR | S_IWUSR);

void display(void){
printk(KERN_ALERT "%s" ,mystring);
}
static int hello(void){
//printk(KERN_ALERT "hello module");
display();
return 0;
} 
static void bye(void){
printk(KERN_ALERT "bye");
}
module_init(hello);
module_exit(bye);

我运行命令make,然后当我运行insmod test.ko mystring="blahblahblah"时,将正确插入该模块,但是当我运行dmesg时,它不会显示"blahblahblah". 在运行rmmod test.kodmseg之后,终端中将出现表达式"blahblahblah".当我再次运行insmod test.ko mystring="blahblahblah"然后dmesg时,将显示"blahblahblah". 到底是什么问题? 是我的问题还是系统?

I run command make and then when I run insmod test.ko mystring="blahblahblah", the module will be inserted correctly but when I run dmesg it doesn't show the "blahblahblah". after I run rmmod test.ko and dmseg the expression "blahblahblah" will appear in the terminal. when I run insmod test.ko mystring="blahblahblah" again and then dmesg the "blahblahblah" will be printed. what is the problem exactly? is it my problem or the system?

有时printk可能会延迟输出(即,消息存储在内部缓冲区中,而不存储在内核日志中).为避免这种情况,请始终在打印的字符串的末尾添加换行符(\n):

Sometimes printk may defer output (that is, message is stored in the intrenal buffer, but not in kernel log). For avoid such behavior, always add newline (\n) at the end of the string printed:

printk(KERN_ALERT "%s\n" ,mystring);