LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1747|回复: 5

编译LDD3的scull模块出现一个怪现象

[复制链接]
发表于 2007-8-5 01:16:21 | 显示全部楼层 |阅读模式
我成功的编译了ldd3, scull目录下面的内容。而且成功的运行了scull_load这个脚本,这个脚本主要工作是建立MKNOD,并操作insmod编译出来的.ko文件。

然后我编了一个应用程序,去测试这支driver.这支driver主要功能是从内存中建一个假想设备,可以进行输入,输出等。

我成功的打开了driver,并成功的写入,但是读出时个报错。我在driver代码里面加了printk语句,出现很奇怪的现象。在scull_read中一部分疑问代码为:
        int quantum = dev->quantum, qset = dev->qset;
        int itemsize = quantum * qset; /* how many bytes in the listitem */
        int item, s_pos, q_pos, rest;
        ssize_t retval = 0;

        printk(KERN_ALERT "enter in read function in module\n");
        printk(KERN_ALERT "dev is %d and devsize is %d\n",(int)dev,dev->size);

        if (down_interruptible(&dev->sem))
                return -ERESTARTSYS;
        printk(KERN_ALERT "enter in read function in module1.1\n");
        printk(KERN_ALERT "*f_pos is %d and dev size is %d\n",*f_pos,dev->size);                       if (*f_pos >= dev->size)
                goto out;

    上面是代码的片断,以及我加的调试信息,下面是调试结果
Aug  5 00:55:46 debian kernel: enter in write function in module
Aug  5 00:55:46 debian kernel: dev is -997005312
Aug  5 00:55:46 debian kernel: devsize is 17,*f_pos is 17
<1>enter in read function in module
Aug  5 00:55:46 debian kernel: dev is -997005312 and devsize is 17
Aug  5 00:55:46 debian kernel: enter in read function in module1.1
Aug  5 00:55:46 debian kernel: *f_pos is 17 and dev size is 0
Aug  5 00:59:07 debian kernel: enter in write function in module
   
    可以看到在 write function中dev->size的值最后读出来是17.然后在外面 的应用软件调用read,在scull_read开头的一个printk即down_interruptible上面的那个printk输出时dev->size也是为17.但是运行了down_interruptible之后下面的printk打出来的dev->size的值就变为0了。

     请问各位大侠,知道为什么吗?或者我应该哪里修改一下。
 楼主| 发表于 2007-8-6 19:52:52 | 显示全部楼层
今天我又把代码改了一下,在scull_read里面把  关于dev->size的if 语句都给comment掉,结果可能正常的运行。但是dev->size的值肯定是变了。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-8-7 00:17:47 | 显示全部楼层
上面第一个问题还没有解决,有没有牛人能指教一下,为什么会变呢?dev->size的值会变了,在前后插了一个down_interruptible(&dev->sema)
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-8-9 22:18:55 | 显示全部楼层
问题已经解决,是其它地方代码写出了问题。另外说一下我的一个发现
printk(KERN_ALERT "*f_pos is %d and dev size is %d\n",*f_pos,dev->size);
这样 写打印后面后面dev->size值就是0,但是dev->size的值没有变,我也不知道为什么。呵呵:)
回复 支持 反对

使用道具 举报

发表于 2007-9-25 21:44:05 | 显示全部楼层
ot@linux-58 scull]# echo ldalkfsajkklj >/dev/scull
[root@linux-58 scull]# cat /dev/scull
enter in read function in module
dev is c7124ad0 and devsize is 14
enter in read function in module1.1
*f_pos is 0 and dev size is 0
*f_pos is 0 and dev size is 14
ldalkfsajkklj
enter in read function in module
dev is c7124ad0 and devsize is 14
enter in read function in module1.1
*f_pos is 14 and dev size is 14

/lib/modules/2.6.6/build M=/phydisk/scull  modules
make[1]: Entering directory `/phydisk/linux-2.6.6'
  CC [M]  /phydisk/scull/main.o
/phydisk/scull/main.c: In function `scull_read':
/phydisk/scull/main.c:324: warning: int format, different type arg (arg 2)
/phydisk/scull/main.c:325: warning: int format, long unsigned int arg (arg 3)

source:
ssize_t scull_read(struct file *filp, char __user *buf, size_t count,
                loff_t *f_pos)
{
        struct scull_dev *dev = filp->private_data;
        struct scull_qset *dptr;        /* the first listitem */
        int quantum = dev->quantum, qset = dev->qset;
        int itemsize = quantum * qset; /* how many bytes in the listitem */
        int item, s_pos, q_pos, rest;
        ssize_t retval = 0;
       
  //  PTRACE();

    printk(KERN_ALERT"enter in read function in module\n");
    printk(KERN_ALERT"dev is %p and devsize is %lu\n",dev,dev->size);
   
        if (down_interruptible(&dev->sem))
                return -ERESTARTSYS;


    printk(KERN_ALERT"enter in read function in module1.1\n");
    printk(KERN_ALERT"*f_pos is %d and dev size is %lu\n",*f_pos,dev->size);
    printk(KERN_ALERT"*f_pos is %lld and dev size is %d\n",*f_pos,dev->size);

编译有警告,输出不正常 ,的确像你说的那样



/lib/modules/2.6.6/build M=/phydisk/scull  modules
make[1]: Entering directory `/phydisk/linux-2.6.6'
  CC [M]  /phydisk/scull/main.o
  LD [M]  /phydisk/scull/scull.o
  Building modules, stage 2.
  MODPOST
  LD [M]  /phydisk/scull/scull.ko
make[1]: Leaving directory `/phydisk/linux-2.6.6'
[root@linux-58 scull]# ./scull_load
scullsingle registered at fe00008
sculluid registered at fe00009
scullwuid registered at fe0000a
sullpriv registered at fe0000b
[root@linux-58 scull]# echo "dsaldfkakfjkfddflkkdakl" >/dev/scull
[root@linux-58 scull]# cat /dev/scull
enter in read function in module
dev is c6f78444 and devsize is 24
enter in read function in module1.1
*f_pos is 0 and dev size is 24
dsaldfkakfjkfddflkkdakl
enter in read function in module
dev is c6f78444 and devsize is 24
enter in read function in module1.1
*f_pos is 24 and dev size is 24
[root@linux-58 scull]#





ssize_t scull_read(struct file *filp, char __user *buf, size_t count,
                loff_t *f_pos)
{
        struct scull_dev *dev = filp->private_data;
        struct scull_qset *dptr;        /* the first listitem */
        int quantum = dev->quantum, qset = dev->qset;
        int itemsize = quantum * qset; /* how many bytes in the listitem */
        int item, s_pos, q_pos, rest;
        ssize_t retval = 0;
       
  //  PTRACE();

    printk(KERN_ALERT"enter in read function in module\n");
    printk(KERN_ALERT"dev is %p and devsize is %d\n",dev,dev->size);
   
        if (down_interruptible(&dev->sem))
                return -ERESTARTSYS;


    printk(KERN_ALERT"enter in read function in module1.1\n");
    printk(KERN_ALERT"*f_pos is %lld and dev size is %lu\n",*f_pos,dev->size);

        if (*f_pos >= dev->size)
                goto out;


编译正常,输出正常

source:

ssize_t scull_read(struct file *filp, char __user *buf, size_t count,
                loff_t *f_pos)
{
        struct scull_dev *dev = filp->private_data;
        struct scull_qset *dptr;        /* the first listitem */
        int quantum = dev->quantum, qset = dev->qset;
        int itemsize = quantum * qset; /* how many bytes in the listitem */
        int item, s_pos, q_pos, rest;
        ssize_t retval = 0;
       
  //  PTRACE();

    printk(KERN_ALERT"enter in read function in module\n");
    printk(KERN_ALERT"dev is %p and devsize is %lu\n",dev,dev->size);
   
        if (down_interruptible(&dev->sem))
                return -ERESTARTSYS;


    printk(KERN_ALERT"enter in read function in module1.1\n");
    printk(KERN_ALERT"*f_pos is %lld and dev size is %d\n",*f_pos,dev->size);
回复 支持 反对

使用道具 举报

发表于 2007-9-25 21:52:30 | 显示全部楼层
不要忽视 warning  ...


至于为什么,我还没有仔细阅读printk源码。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表