我正在为 Linux 内核编写一个系统调用,到目前为止,它还没有按照我的预期工作(这并不奇怪)。我正在尝试使用printk
消息在一个函数中调试它,但它们不起作用。然而,单独函数中的那些确实有效。
我尝试使用pr_info
而不是printk
,但这没有奏效。我用mdelay
而不是msleep
,这没有帮助。
printk 在dq_update
中不起作用(在sys_delta
中称为),但它确实在dq_print
中工作。
typedef struct _delta_entry
{
struct task_struct *task;
struct list_head list;
int delta_time;
} delta_entry;
void dq_print(delta_entry* head)
{
delta_entry* pos;
printk("dq_print: ");
list_for_each_entry(pos, &head->list, list)
{
printk("[%d] ", pos->delta_time);
}
printk("n");
}
void dq_update(struct list_head *delta_queue)
{
//find and store the root
delta_entry *root = list_entry(delta_queue -> prev, delta_entry, list);
//decrement its delta time
pr_info("DECREMENTING ROOT FROM %d", root->delta_time);
root -> delta_time -= 1;
pr_info("DONE DECREMENTING ROOT: AT %d", root->delta_time);
//remove it if its waiting is done, and any subsequent processes that were waiting for the same time
for (root; root->delta_time <= 0 && root != delta_queue; root = list_entry(root->list.prev, delta_entry, list))
{
wake_up_process(root->task);
delta_queue -> prev = delta_queue -> prev -> prev;
delta_queue -> prev -> next = delta_queue;
}
}
asmlinkage long sys_delta(void)
{
int sleep_times[] = {532, 58, 542, 573, 924, 379, 753, 183, 268, 254, 803, 188, 794, 936, 976, 585, 755, 189, 905, 880, 911, 396, 889, 348, 629, 515, 830, 107, 452, 47, 857, 650, 14, 524, 548, 476, 551, 953, 366, 572, 419, 450, 134, 748, 944, 904, 557, 651, 788, 92, 982, 901, 11, 5, 72, 798, 447, 658, 843, 445, 204, 380, 392, 385, 199, 426, 474, 139, 404, 274, 511, 74, 540, 244, 827, 330, 342, 598, 487, 206, 606, 261, 81, 772, 603, 323, 920, 430, 67, 316, 706, 801, 716, 307, 703, 657, 228, 712, 434, 898};
delta_entry head = {NULL, LIST_HEAD_INIT(head.list), NULL};
delta_entry entries[100];
int i;
for (i = 0; i < 100; i++)
{
delta_entry de = {make_thread(sleep_times[i]), LIST_HEAD_INIT(de.list), sleep_times[i]};
entries[i] = de;
dq_add(&entries[i], entries[i].delta_time, &head);
}
i = 1;
do
{
mdelay(1);
dq_update(&head.list);
i++;
}
while (dq_size(&head.list) > 0);
dq_print(&head);
return 0;
}
我知道这是很多代码,对不起,如果我发布太多或这个问题不好。感谢您的帮助!
如果 dq_update() 的代码真的被执行了,那么你调用 printk 的优先级可能太低。通过从根 shell 运行"echo 8>/proc/sys/kernel/printk"来提高内核日志级别,或者将日志级别添加到 printk() 调用中,例如: printk(KERN_EMERG"重要消息"); 您可以在 中查看可能的日志级别值列表: include/linux/kern_levels.h