Linux驱动 | debugfs接口创建

简介: Linux驱动 | debugfs接口创建

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

实现效果

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

前期准备

内核配置打开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");

运行结果:

读写字符串:

#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");

运行结果:

函数接口说明

创建目录、文件函数:

/* 创建目录 */
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

end

猜你喜欢

Linux驱动 | procfs接口创建

Linux驱动 | 在驱动中创建sysfs接口

Linux reset子系统及驱动实例

Linux clock子系统及驱动实例

Linux内核中常用的C语言技巧

Linux内核死锁检测工具——Lockdep

Linux内核基础篇——常用调试技巧汇总

Linux内核基础篇——动态输出调试

Linux内核基础篇——printk调试

Linux内核基础篇——initcall

写给新手的MMU工作原理


相关文章
|
2月前
|
Linux API 数据安全/隐私保护
【Linux 用户管理】Linux用户身份信息获取与管理API 接口
【Linux 用户管理】Linux用户身份信息获取与管理API 接口
29 0
|
2月前
|
Linux API 调度
Linux系统驱动跟裸机驱动的区别
Linux系统驱动跟裸机驱动的区别
33 0
|
2月前
|
域名解析 网络协议 Linux
【Shell 命令集合 网络通讯 】Linux 设置和管理网络接口配置信息 netconfig命令 使用指南
【Shell 命令集合 网络通讯 】Linux 设置和管理网络接口配置信息 netconfig命令 使用指南
64 1
|
2月前
|
存储 缓存 Linux
【Shell 命令集合 磁盘维护 】Linux 设置和查看硬盘驱动器参数 hdparm命令使用教程
【Shell 命令集合 磁盘维护 】Linux 设置和查看硬盘驱动器参数 hdparm命令使用教程
39 0
|
29天前
|
网络协议 Linux
在Linux中,管理和配置网络接口
在Linux中管理网络接口涉及多个命令,如`ifconfig`(在新版本中被`ip`取代)、`ip`(用于网络设备配置)、`nmcli`(NetworkManager的CLI工具)、`nmtui`(文本界面配置)、`route/ip route`(处理路由表)、`netstat/ss`(显示网络状态)和`hostnamectl/systemctl`(主机名和服务管理)。这些命令帮助用户启动接口、设置IP地址、查看连接和路由信息。不同发行版可能有差异,建议参考相应文档。
20 4
|
2月前
|
监控 网络协议 Linux
【Shell 命令集合 网络通讯 】Linux 显示网络 连接、路由表和网络接口信息 netstat命令 使用指南
【Shell 命令集合 网络通讯 】Linux 显示网络 连接、路由表和网络接口信息 netstat命令 使用指南
70 1
|
1天前
|
存储 算法 网络协议
【探索Linux】P.26(网络编程套接字基本概念—— socket编程接口 | socket编程接口相关函数详细介绍 )
【探索Linux】P.26(网络编程套接字基本概念—— socket编程接口 | socket编程接口相关函数详细介绍 )
6 0
|
1天前
|
Linux
linux驱动层输出dev_dbg打印信息
linux驱动层输出dev_dbg打印信息
3 0
|
2天前
|
网络协议 Linux 开发工具
Linux中 /etc/sysconfig/network-scripts/ifcfg-<interface> 网络接口配置 详解 看这一篇够用
Linux中 /etc/sysconfig/network-scripts/ifcfg-<interface> 网络接口配置 详解 看这一篇够用
|
7天前
|
Ubuntu 网络协议 Linux

热门文章

最新文章