毕业论文开发语言企业开发JAVA技术.NET技术WEB开发Linux/Unix数据库技术Windows平台移动平台嵌入式论文范文英语论文
您现在的位置: 毕业论文 >> linux >> 正文

嵌入式Linux中copy_to_user中Segmentation fault

更新时间:2013-10-14:  来源:毕业论文

其实这个代码与具体开发板无关,想在内核空间和用户空间传递结构体数据,第一块的write已经验证成功,但是read一直报Segmentation fault错误,已经调了好几天。想请教大家,谢谢大家,具体代码如下:

驱动代码:

C/C++ code?#include <linux/init.h>   #include <linux/module.h>   #include <linux/device.h> #include <linux/kdev_t.h> #include <linux/timer.h> #include <asm/uaccess.h> #include <asm/io.h>  #include <linux/fs.h>   /*       Kernel Version: Linux 2.6.38       Arm Version: Mini 6410 */  #define MyPrintk  printk   struct my_data_structure {     unsigned int number;     char *name;     unsigned long count; };   static dev_t  Leds_Major ; static char * DEVICE_NAME = "DataTransfemission"; static struct class *leds_class; static struct my_data_structure my_own_data[] = {             {1,"Jack",0},             {2,"Tom",0},             {3,"Kobe",0},             {4,"Xiaohong",0}     };   static ssize_t data_read(struct file *file, char __user * buffer, size_t count, loff_t *pos) {     struct my_data_structure temp_data=              {6,"Jack",0};     MyPrintk("123\n");     int ret = copy_to_user(buffer,(void *)&temp_data, sizeof(temp_data));     if (ret != 0){         MyPrintk("read error\n");     }     return 0; }   static ssize_t data_write(struct file *flie, const char __user * buffer, size_t count, loff_t *pos) {     struct my_data_structure user_data;     int ret = copy_from_user((void *)&user_data, buffer, count);     if (ret != 0){         MyPrintk("write error\n");     }     MyPrintk (KERN_EMERG "Kernel from user data: %i, %s, %lu\n", user_data.number,              user_data.name, user_data.count);       return 0; }   static int data_open(struct file *file, struct node *nodes) {     return 0; }   static struct file_operations  s3c64XX_leds_fops = {     .owner = THIS_MODULE,     .read = data_read,     .write = data_write,     .open = data_open, };   static int myleds_init(void)   {        Leds_Major = register_chrdev(Leds_Major,DEVICE_NAME , &s3c64XX_leds_fops);     if(Leds_Major < 0){               MyPrintk (KERN_EMERG "Sorry, Can not register the data trsanmission device!\n");          }      MyPrintk (KERN_EMERG " Register the data trsanmission leds device\n");          leds_class = class_create(THIS_MODULE, DEVICE_NAME);      device_create(leds_class, NULL , MKDEV(Leds_Major, 0), NULL,DEVICE_NAME);     return 0;   }     static void myleds_exit(void)   {        unregister_chrdev(Leds_Major, DEVICE_NAME);       device_destroy(leds_class,  MKDEV(Leds_Major, 0));         class_destroy(leds_class);       MyPrintk (KERN_EMERG "Data transmission Linux Byebye\n");   }       module_init(myleds_init);   module_exit(myleds_exit); MODULE_LICENSE("GPL");  

 

应用层代码如下

C/C++ code?#include<stdio.h> #include<fcntl.h> #include<unistd.h> #include<string.h> #include <stdlib.h>   struct my_data_structure {     unsigned int number;     char *name;     unsigned long count; };   int main(int argc, char **argv) {     int fd;     int ret;     fd = open("/dev/DataTransfemission", O_RDWR);     if (fd < 0){         printf("Sorry, can't open!\n");     }           struct my_data_structure my_write_data = {6,"weipeng",1};     printf("struct sizeof %d, %d\n", sizeof(my_write_data ), sizeof(struct my_data_structure ));     ret = write(fd, &my_write_data , sizeof(my_write_data));     if(ret < 0){         printf("Sorry, write2 error!\n");     }       struct my_data_structure *my_write_data2 = (struct my_data_structure*)malloc(sizeof(struct my_data_structure));     printf("struct* sizeof %d, %d\n", sizeof(my_write_data2), sizeof(struct my_data_structure ));     my_write_data2->number = 5;     my_write_data2->name ="xiangwei";     my_write_data2->count = 2;     ret = write(fd, my_write_data2 , sizeof(struct my_data_structure));     if(ret < 0){         printf("Sorry, write2 error!\n");     }         printf("read 1!\n");     struct my_data_structure my_read_data;     ret = read(fd, &my_read_data , sizeof(my_read_data));     if(ret < 0){         printf("Sorry, read1 error!\n");     }     printf("my_read_data: %ui, %s, %ul\n", my_read_data.number, my_read_data.name, my_read_data.count);           printf("read 3!\n");     struct my_data_structure* my_read_data3 =(struct my_data_structure*)malloc(sizeof(struct my_data_structure));     ret = read(fd, my_read_data3 , sizeof(struct my_data_structure));     if(ret < 0){         printf("Sorry, read3 error!\n");     }     printf("my_read_data3: %ui, %s, %ul\n", my_read_data3->number, my_read_data3->name, my_read_data3->count);         printf("read 2!\n");     struct my_data_structure* my_read_data2;     ret = read(fd, my_read_data2 , sizeof(struct my_data_structure));     if(ret < 0){         printf("Sorry, read2 error!\n");     }     printf("my_read_data2: %ui, %s, %ul\n", my_read_data2->number, my_read_data2->name, my_read_data2->count);               free(my_write_data2);     free(my_read_data2);     free(my_read_data3);     return 0; }
我没调试你的程序,但你的程序肯定是不能运行的,
printf("my_read_data: %ui, %s, %ul\n", my_read_data.number, my_read_data.name, my_read_data.count);肯定要段错误的
my_read_data.name的地址在内核
struct my_data_structure{    unsigned int number;    char *name;    unsigned long count;};
这样的定义是不能将name的数据传送到用户端
你需要把name改成char数组。

设为首页 | 联系站长 | 友情链接 | 网站地图 |

copyright©youerw.com 优尔论文网 严禁转载
如果本毕业论文网损害了您的利益或者侵犯了您的权利,请及时联系,我们一定会及时改正。