首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未能使用不使用调试器的copy_to_user获取内核数据

未能使用不使用调试器的copy_to_user获取内核数据
EN

Stack Overflow用户
提问于 2020-04-03 07:46:37
回答 1查看 125关注 0票数 0

我正在尝试实现简单的调试器接口模块。附代码以供参考。为了编写数据,我使用了copied 8‘> /sys/kernel/debug/debugexercise/text,它的工作原理是将数据复制到内核缓冲区中。但是,当我尝试使用cat命令/sys/kernel/debug/debugexercise/text ( cat )检索数据时,它不会在终端上打印任何数据。

我也尝试使用simple_read_from_buffer而不是copy_to_user,但是得到了相同的结果。任何人都知道这段代码有什么问题。4.13.0-45-泛型是我的系统的核心版本。

代码语言:javascript
复制
#include <linux/module.h>
#include <linux/debugfs.h>
#include <linux/fs.h>
#include <linux/uaccess.h>

#define LEN 512 

static struct dentry *test_dir;
static struct dentry *test_file;

static char ker_buf[LEN] ;
/* read file operation */
static ssize_t test_read(struct file *fp, char __user *user_buffer, size_t count, loff_t *position){
    printk(KERN_NOTICE "debugfs_read called, count %d\n", count);
    return copy_to_user(user_buffer, ker_buf, LEN);

}

static ssize_t test_write(struct file *fp, const char __user *user_buffer, size_t count, loff_t *position){
    printk(KERN_NOTICE "debugfs_write called, count %d\n",count);
    if(count > LEN )
        return -EINVAL;

    copy_from_user(ker_buf, user_buffer, count);
    printk(KERN_NOTICE "write buffer complete: %s\n",ker_buf);
    return count;
}

static struct file_operations fops_debug = {
    .read = test_read,
    .write = test_write,
};

static int __init init_debug(void)
{
    test_dir = debugfs_create_dir("debugexercise", NULL);
    if(NULL == test_dir){
        printk(KERN_ERR "debugfs_create_dir() Failed\n");
        return -1;
    }
    else
        printk(KERN_NOTICE "debugexercise created\n");

    test_file = debugfs_create_file("text", 0644, test_dir, NULL, &fops_debug);
    if(NULL == test_file){
        printk(KERN_ERR "debugfs_create_file() Failed\n");
        debugfs_remove(test_dir);
        return -1;
    }
    else
        printk(KERN_NOTICE "text under debugexercise created\n");

    return 0;
}

static void __exit exit_debug(void)
{
    printk(KERN_NOTICE "removing module\n");
    debugfs_remove(test_file);
    debugfs_remove(test_dir);
}
module_init(init_debug)
module_exit(exit_debug)
MODULE_LICENSE("GPL");
EN

回答 1

Stack Overflow用户

发布于 2020-04-03 09:37:04

copy_to_user返回无法复制的字节数。就成功而言,这将是零。因此,猫会显示0个字符。我认为你应该这样做:

代码语言:javascript
复制
if (copy_to_user(user_buffer, ker_buf, LEN)){
         printk(KERN_INFO "copy to user failed.\n");
         return -EINVAL; /* For instance ... */
}

return LEN;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61007236

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档