为什么我的内核日志没有显示最新的输出? [英] why my kernel log is not showing the latest output?

查看:98
本文介绍了为什么我的内核日志没有显示最新的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在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);

这篇关于为什么我的内核日志没有显示最新的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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