Linux 内核模块:printk 消息不在缓冲区日志中我期望的位置

Linux Kernel module: printk message not where I expect to be in the buffer log

此问题与 lab/homework 作业有关。我们被要求学习如何加载和删除内核模块,然后修改已提供的内核模块中的 C 代码。我们必须定义一个结构,其中包含一些要插入到链表中的元素。然后我们必须通过 printk() 使用链表中的数据打印一些消息。

这是我的代码:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/list.h>
#include <linux/types.h>


/*Defines a struct containing the elements that are to be
inserted in the linked list*/
struct birthday{
    int day;
    int month;
    int year;
    struct list_head list;
};

/*Declares a list_head object */
static LIST_HEAD(birthday_list);

/* This function is called when the module is loaded. */
int simple_init(void) { 
    /* Create and initialize instances of struct birthday*/
    struct birthday *person, *ptr;
    int count;

    printk(KERN_INFO "Loading Module\n");

    /* This loop populates the linked list with 5 nodes,
     * each with different birthday data*/
    for (count = 0; count < 5  ; count++){
        person = kmalloc(sizeof(*person), GFP_KERNEL);
        person -> day = count + 2;
        person -> month = 11 - (count + 2);
        person -> year = 1985 + ((count + 1) * 3);
        INIT_LIST_HEAD(&person -> list);
        list_add_tail(&person -> list, &birthday_list);
    }

    /*Traverse the list*/
    list_for_each_entry(ptr, &birthday_list, list) {
        printk(KERN_INFO "Birthday: Month %d Day %d Year %d\n", ptr -> month, ptr -> day, ptr -> year);
    }
    return 0;
}

/* This function is called when the module is removed. */
void simple_exit(void) {
    struct birthday *ptr, *next;

    printk(KERN_INFO "Removing Module\n");  

    list_for_each_entry_safe(ptr, next, &birthday_list, list){
        printk(KERN_INFO "Removing %d %d %d", ptr->month,     ptr->day, ptr->year);
        list_del(&ptr -> list);
        kfree(ptr);
    }
}

/* Macros for registering module entry and exit points. */
module_init( simple_init );
module_exit( simple_exit );


MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Simple Module");
MODULE_AUTHOR("SGG");

我可以毫无错误地编译代码,它似乎可以很好地加载和删除。我的问题是打印的报表。当我加载模块并使用 dmesg 检查日志缓冲区时,我可以看到所有 5 条消息(链表中的每个节点一条)。当我移除模块并检查日志缓冲区时,我只看到 4 条消息。此时不打印与链表中第 5 个节点关联的消息。如果我再次加载模块,然后检查日志缓冲区,第一条消息就是我删除模块时应该在日志中的消息。

我只有在有 5 个节点时遇到过这个问题。如果我更改我的循环以创建 10 个节点,所有消息都会按我的预期打印。 6个节点或4个节点也是如此。但是每次我创建 5 个节点时,我都会遇到这个问题。

这是我的输出:Screenshot of my output from command line

此作业的所有编辑和操作都在 Oracle VM VirtualBox 中完成,运行 在 Windows 10. Linux 内核和我正在修改的内核模块作为课程材料和教科书的一部分提供。

非常感谢任何帮助!

这是因为您在 printk 末尾遗漏了 '\n'。