Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

c - Why my kernel log is not showing the latest output?

I'm coding a simple kernel module, in 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);

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

printk(KERN_ALERT "%s
" ,mystring);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...