【问题标题】:Why my kernel log is not showing the latest output?为什么我的内核日志没有显示最新的输出?
【发布时间】:2018-05-15 07:28:33
【问题描述】:

我正在编写一个简单的内核模块,在 Ubuntu 17.04 中,它接受一个字符串并将其打印在内核日志中。

#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

到底是什么问题?是我的问题还是系统的问题?

【问题讨论】:

    标签: c linux linux-kernel kernel kernel-module


    【解决方案1】:

    有时printk 可能会延迟输出(也就是说,消息存储在内部缓冲区中,但不存储在内核日志中)。为避免此类行为,请始终在打印的字符串末尾添加换行符 (\n):

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

    【讨论】:

      猜你喜欢
      • 2013-12-17
      • 2020-09-17
      • 2020-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      • 2020-09-26
      • 2020-04-15
      相关资源
      最近更新 更多