目錄
代码实现 " >代码实现
读写变量:" >读写变量:
读写字符串:" >读写字符串:
函数接口说明 " >函数接口说明
首頁 運維 linux運維 Linux驱动 | debugfs接口创建

Linux驱动 | debugfs接口创建

Jul 31, 2023 pm 05:39 PM
linux

点击上方“嵌入式Linux充电站”,选择“置顶/星标公众号”

福利干货,第一时间送达
Linux驱动 | debugfs接口创建

上篇介绍了procfs接口的创建,今天再介绍一种debugfs接口的创建。

实现效果

/sys/kernel/debug/目录下创建一个ion/test文件,通过catecho的方式进行读写操作:

Linux驱动 | debugfs接口创建
Linux驱动 | debugfs接口创建

前期准备

内核配置打开debugfs:

CONFIG_DEBUG_FS=y
登入後複製

挂载debugfs文件系统:

mount -t debugfs none /sys/kernel/debug
登入後複製

代码实现

读写变量:

#include <linux/debugfs.h>
#include <linux/module.h>
#include <linux/types.h>

static struct dentry *ion_dir;
static u64 test_u64 = 0;

static int __init debugfs_init(void)
{

    //创建一个/sys/kernel/debug/ion目录
    ion_dir = debugfs_create_dir("ion", NULL);
    if (!ion_dir) {
        printk("ion_dir is null\n");
        return -1;
    }

    /* 创建/sys/kernel/debug/ion/test_u64文件 */
    debugfs_create_u64("test_u64", 0644,
                        ion_dir, &test_u64);

    return 0;
}

static void __exit debugfs_exit(void)
{
    debugfs_remove_recursive(ion_dir);
}

module_init(debugfs_init);
module_exit(debugfs_exit);
MODULE_LICENSE("GPL");
登入後複製

运行结果:

Linux驱动 | debugfs接口创建

读写字符串:

#include <linux/debugfs.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/errno.h>
#include <linux/dcache.h>
#include <linux/types.h>

static char ion_buf[512] = "hello\n";
static struct dentry *ion_dir;

static int ion_open(struct inode *inode, struct file *filp)
{
    //printk("ion open\n");
    return 0;
}

ssize_t ion_read(struct file *filp, char __user *buf, size_t count, loff_t *offp)
{
    int retval = 0;
    if ((*offp + count) > 512)
        count = 512 - *offp;

    if (copy_to_user(buf, ion_buf+*offp, count)) {
        printk("copy to user failed, count:%ld\n", count);
        retval = -EFAULT;
        goto out;
    }
    *offp += count;
    retval = count;
out:
    return retval;
}

ssize_t ion_write(struct file *filp, const char __user *buff, size_t count, loff_t *offp)
{
    int retval;

    if (*offp > 512)
        return 0;

    if (*offp + count > 512)
        count = 512 - *offp;

    if (copy_from_user(ion_buf+*offp, buff, count)) {
        printk("copy from user failed, count:%ld\n", count);
        retval = -EFAULT;
        goto out;
    }
    *offp += count;
    retval = count;
out:
    return retval;
}

struct file_operations my_fops = {
    .owner = THIS_MODULE,
    .read = ion_read,
    .write = ion_write,
    .open = ion_open,
};

static int __init debugfs_init(void)
{
    printk("INIT MODULE\n");

    //创建一个/sys/kernel/debug/ion目录
    ion_dir = debugfs_create_dir("ion", NULL);
    if (!ion_dir) {
        printk("ion_dir is null\n");
        return -1;
    }

    /* 创建/sys/kernel/debug/ion/test文件 */
    struct dentry *filent = debugfs_create_file("test", 0644, ion_dir, NULL, &my_fops);
    if (!filent) {
        printk("test file is null\n");
        return -1;
    }

    return 0;
}

static void __exit debugfs_exit(void)
{
    debugfs_remove_recursive(ion_dir);
}

module_init(debugfs_init);
module_exit(debugfs_exit);
MODULE_LICENSE("GPL");
登入後複製

运行结果:

Linux驱动 | debugfs接口创建

函数接口说明

创建目录、文件函数:

/* 创建目录 */
struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);

/*创建节点 */
struct dentry *debugfs_create_file(const char *name, umode_t mode,
                                   struct dentry *parent, void *data,
                                   const struct file_operations *fops);
登入後複製

name:要创建的/sys/kernel/debug下的目录名

parent:父目录,用struct dentry结构体表示。如果直接在/sys/kernel/debug/下创建文件,则为NULL

创建不同大小的文件:

//创建十进制的无符号文件
void debugfs_create_u8(const char *name, umode_t mode,
                       struct dentry *parent, u8 *value);
void debugfs_create_u16(const char *name, umode_t mode,
                        struct dentry *parent, u16 *value);
void debugfs_create_u32(const char *name, umode_t mode,
                        struct dentry *parent, u32 *value);
void debugfs_create_u64(const char *name, umode_t mode,
                        struct dentry *parent, u64 *value);
//创建十六进制的无符号文件
void debugfs_create_x8(const char *name, umode_t mode,
                       struct dentry *parent, u8 *value);
void debugfs_create_x16(const char *name, umode_t mode,
                        struct dentry *parent, u16 *value);
void debugfs_create_x32(const char *name, umode_t mode,
                        struct dentry *parent, u32 *value);
void debugfs_create_x64(const char *name, umode_t mode,
                        struct dentry *parent, u64 *value);
登入後複製

更详细的debugfs用法请参考官方文档:Documentation/filesystems/debugfs.txt

以上是Linux驱动 | debugfs接口创建的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Android TV Box 獲得非官方 Ubuntu 24.04 升級 Android TV Box 獲得非官方 Ubuntu 24.04 升級 Sep 05, 2024 am 06:33 AM

Android TV Box 獲得非官方 Ubuntu 24.04 升級

deepseek網頁版入口 deepseek官網入口 deepseek網頁版入口 deepseek官網入口 Feb 19, 2025 pm 04:54 PM

deepseek網頁版入口 deepseek官網入口

deepseek怎麼安裝 deepseek怎麼安裝 Feb 19, 2025 pm 05:48 PM

deepseek怎麼安裝

BitPie比特派錢包app下載位址 BitPie比特派錢包app下載位址 Sep 10, 2024 pm 12:10 PM

BitPie比特派錢包app下載位址

BITGet官方網站安裝(2025新手指南) BITGet官方網站安裝(2025新手指南) Feb 21, 2025 pm 08:42 PM

BITGet官方網站安裝(2025新手指南)

詳解:Shell腳本變數判斷參數指令 詳解:Shell腳本變數判斷參數指令 Sep 02, 2024 pm 03:25 PM

詳解:Shell腳本變數判斷參數指令

Zabbix 3.4 原始碼編譯安裝 Zabbix 3.4 原始碼編譯安裝 Sep 04, 2024 am 07:32 AM

Zabbix 3.4 原始碼編譯安裝

歐易okx安裝包直接進 歐易okx安裝包直接進 Feb 21, 2025 pm 08:00 PM

歐易okx安裝包直接進

See all articles