Linux内核模块中的printk,c - Linux内核模块:printk消息不在缓冲区日志中我期望的位置[重复] - 堆栈内存溢出...

博主在学习加载和删除内核模块的过程中,遇到一个问题:当创建5个链表节点并在模块卸载时删除它们时,日志缓冲区只显示4个节点的消息。这个问题在创建更多或更少节点时不会出现。博主怀疑可能与日志缓冲区满或打印顺序有关,但具体原因不明。
摘要由CSDN通过智能技术生成

这个问题已经在这里有了答案:

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

这是我的代码:

#include

#include

#include

#include

#include

#include

/*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个节点时,都会遇到此问题。

这是我的输出: 命令行输出的屏幕截图

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

任何帮助是极大的赞赏!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值