0
我正在寫一個字符設備內核模塊,並且遇到了一個非常奇怪的問題。我將變量設置爲某些值,但當我使用printk()打印它們時,我沒有收到這些值。這裏是我的代碼(過度的printk):內核模塊沒有在llseek中分配變量
loff_t llseek(struct file *filp, loff_t off, int whence)
{
printk(KERN_NOTICE "In llseek\n");
printk(KERN_NOTICE "filp->f_pos = 0x%llX\n",filp->f_pos);
printk(KERN_NOTICE "off = 0x%llX\n", off);
loff_t newpos;
switch(whence) {
case 0: /* SEEK_SET */
printk(KERN_NOTICE "SEEK_SET\n");
newpos = off;
printk(KERN_NOTICE "1] newpos = 0x%X\n",newpos);
printk(KERN_NOTICE "2] newpos = 0x%X\n",newpos);
break;
case 1: /* SEEK_CUR */
newpos = filp->f_pos + off;
printk(KERN_NOTICE "SEEK_CUR\n");
break;
case 2: /* SEEK_END */
newpos = 0x1000 + off;
printk(KERN_NOTICE "SEEK_END\n");
break;
default: /* can't happen */
return -EINVAL;
}
printk(KERN_NOTICE "3] newpos = 0x%X\n",newpos);
if (newpos < 0) return -EINVAL;
printk(KERN_NOTICE "4] newpos = 0x%X\n",newpos);
filp->f_pos = newpos;
printk(KERN_NOTICE "5] newpos = 0x%lX\n", newpos);
return newpos;
}
這是從用戶空間調用此:
lseek(dev_fd, 0x90, 0);
這裏是控制檯輸出:
[ 53.764351] In llseek
[ 53.766208] filp->f_pos = 0x0
[ 53.768629] off = 0x90
[ 53.770560] SEEK_SET
[ 53.772341] 1] newpos = 0xC139D40C
[ 53.775117] 2] newpos = 0x0
[ 53.777395] 3] newpos = 0x0
[ 53.779680] 4] newpos = 0x0
[ 53.781959] 5] newpos = 0xC139D40C
我會預計產量爲:
[ 53.764351] In llseek
[ 53.766208] filp->f_pos = 0x0
[ 53.768629] off = 0x90
[ 53.770560] SEEK_SET
[ 53.772341] 1] newpos = 0x90
[ 53.775117] 2] newpos = 0x90
[ 53.777395] 3] newpos = 0x90
[ 53.779680] 4] newpos = 0x90
[ 53.781959] 5] newpos = 0x90
爲什麼會newpos
變化與printk的