0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

Linux驅(qū)動debugfs接口代碼實現(xiàn)

麥辣雞腿堡 ? 來源:嵌入式Linux充電站 ? 作者:Vincent ? 2023-09-27 11:12 ? 次閱讀

實現(xiàn)效果

/sys/kernel/debug/目錄下創(chuàng)建一個ion/test文件,通過cat、echo的方式進行讀寫操作:

圖片

圖片

前期準備

內(nèi)核配置打開debugfs:

CONFIG_DEBUG_FS=y

掛載debugfs文件系統(tǒng):

mount -t debugfs none /sys/kernel/debug

代碼實現(xiàn)

讀寫變量:

#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)
{

    //創(chuàng)建一個/sys/kernel/debug/ion目錄
    ion_dir = debugfs_create_dir("ion", NULL);
    if (!ion_dir) {
        printk("ion_dir is nulln");
        return -1;
    }

    /* 創(chuàng)建/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");

運行結(jié)果:

圖片

讀寫字符串:

#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] = "hellon";
static struct dentry *ion_dir;

static int ion_open(struct inode *inode, struct file *filp)
{
    //printk("ion openn");
    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:%ldn", 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:%ldn", 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 MODULEn");

    //創(chuàng)建一個/sys/kernel/debug/ion目錄
    ion_dir = debugfs_create_dir("ion", NULL);
    if (!ion_dir) {
        printk("ion_dir is nulln");
        return -1;
    }

    /* 創(chuàng)建/sys/kernel/debug/ion/test文件 */
    struct dentry *filent = debugfs_create_file("test", 0644, ion_dir, NULL, &my_fops);
    if (!filent) {
        printk("test file is nulln");
        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");

運行結(jié)果:

圖片

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學習之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • 接口
    +關(guān)注

    關(guān)注

    33

    文章

    8257

    瀏覽量

    149955
  • 驅(qū)動
    +關(guān)注

    關(guān)注

    12

    文章

    1790

    瀏覽量

    84909
  • Linux
    +關(guān)注

    關(guān)注

    87

    文章

    11123

    瀏覽量

    207912
  • 代碼
    +關(guān)注

    關(guān)注

    30

    文章

    4671

    瀏覽量

    67767
收藏 人收藏

    評論

    相關(guān)推薦

    Linux驅(qū)動中創(chuàng)建procfs接口的方法

    上篇介紹了Linux驅(qū)動中sysfs接口的創(chuàng)建,今天介紹procfs接口的創(chuàng)建。
    發(fā)表于 05-31 16:48 ?670次閱讀
    <b class='flag-5'>Linux</b><b class='flag-5'>驅(qū)動</b>中創(chuàng)建procfs<b class='flag-5'>接口</b>的方法

    Linux驅(qū)動中創(chuàng)建debugfs接口的方法

    上篇介紹了procfs接口的創(chuàng)建,今天再介紹一種debugfs接口的創(chuàng)建。
    發(fā)表于 05-31 16:53 ?918次閱讀
    <b class='flag-5'>Linux</b><b class='flag-5'>驅(qū)動</b>中創(chuàng)建<b class='flag-5'>debugfs</b><b class='flag-5'>接口</b>的方法

    如何去實現(xiàn)嵌入式LINUX驅(qū)動的軟件代碼

    如何對嵌入式LINUX驅(qū)動的硬件信息進行配置呢?如何去實現(xiàn)嵌入式LINUX驅(qū)動的軟件代碼呢?
    發(fā)表于 12-24 07:31

    Linux MTD 源代碼分析

    Linux MTD 源代碼分析 Linux MTD介紹:設(shè)備層和原始設(shè)備層的函數(shù)調(diào)用關(guān)系(紅色部分需要我們實現(xiàn)):NOR型Flash芯片驅(qū)動
    發(fā)表于 02-08 16:43 ?9次下載

    Linux內(nèi)核源代碼漫游

    Linux內(nèi)核源代碼漫游 本章試圖以順序的方式來解釋Linux代碼,以幫助讀者對源代碼的體系結(jié)構(gòu)以及很多相關(guān)的unix特性的
    發(fā)表于 02-09 15:27 ?26次下載

    基于Linux下的LCD驅(qū)動程序實現(xiàn)

    基于Linux下的LCD驅(qū)動程序實現(xiàn)
    發(fā)表于 10-30 16:45 ?12次下載
    基于<b class='flag-5'>Linux</b>下的LCD<b class='flag-5'>驅(qū)動</b>程序<b class='flag-5'>實現(xiàn)</b>

    你知道Linux內(nèi)核里的DebugFS?

    DebugFS,顧名思義,是一種用于內(nèi)核調(diào)試的虛擬文件系統(tǒng),內(nèi)核開發(fā)者通過debugfs和用戶空間交換數(shù)據(jù)。
    發(fā)表于 04-25 18:55 ?1846次閱讀
    你知道<b class='flag-5'>Linux</b>內(nèi)核里的<b class='flag-5'>DebugFS</b>?

    要學會調(diào)試內(nèi)核打印debugfs

    name是創(chuàng)建的目錄名字,parent是該目錄的父目錄。如果填NULL,則直接出現(xiàn)在debugfs的根目錄。
    發(fā)表于 04-27 19:01 ?1146次閱讀

    嵌入式Linux系統(tǒng)的驅(qū)動原理和使用ARM Linux實現(xiàn)SPI驅(qū)動程序的說明

    介紹嵌入式Linux系統(tǒng)的驅(qū)動原理;分析SPI協(xié)議的通信原理和微處理器S3C2440A中SPI接口的硬件結(jié)構(gòu);闡述SPI驅(qū)動程序的實現(xiàn)過程。
    發(fā)表于 11-14 16:36 ?11次下載
    嵌入式<b class='flag-5'>Linux</b>系統(tǒng)的<b class='flag-5'>驅(qū)動</b>原理和使用ARM <b class='flag-5'>Linux</b><b class='flag-5'>實現(xiàn)</b>SPI<b class='flag-5'>驅(qū)動</b>程序的說明

    linux spi應用層驅(qū)動以及回環(huán)測試代碼

    linux spi應用層驅(qū)動以及回環(huán)測試代碼
    發(fā)表于 10-22 15:47 ?2次下載

    linux系統(tǒng)的驅(qū)動實現(xiàn)原理

    原理就是將硬件操作的接口全都放到驅(qū)動鏈表上,在驅(qū)動實現(xiàn)device的open、read、write等操作。當然這樣做也有弊端,就是驅(qū)動fi
    發(fā)表于 11-02 09:59 ?822次閱讀

    Linux的PWM驅(qū)動框架及實現(xiàn)方法

    本文主要講述了Linux的PWM驅(qū)動框架、實現(xiàn)方法、驅(qū)動添加方法和調(diào)試方法。
    的頭像 發(fā)表于 05-14 15:24 ?1271次閱讀
    <b class='flag-5'>Linux</b>的PWM<b class='flag-5'>驅(qū)動</b>框架及<b class='flag-5'>實現(xiàn)</b>方法

    Linux內(nèi)核代碼60%都是驅(qū)動?

    為什么Linux內(nèi)核代碼60%都是驅(qū)動? 如果每支持新的設(shè)備就加入驅(qū)動,內(nèi)核會不會變得越來越臃腫?
    的頭像 發(fā)表于 07-11 11:48 ?728次閱讀
    <b class='flag-5'>Linux</b>內(nèi)核<b class='flag-5'>代碼</b>60%都是<b class='flag-5'>驅(qū)動</b>?

    linux內(nèi)核中的debugfs該怎樣去使用呢?

    debugfs可用于內(nèi)核向用戶空間提供信息,debugfs是個小型的文件系統(tǒng),與/proc和sysfs不同,debugfs沒有較為嚴苛的規(guī)則和定義,我們可以在里面放置想要的任何信息,以便于系統(tǒng)開發(fā)和調(diào)試。
    的頭像 發(fā)表于 08-21 09:01 ?3055次閱讀
    <b class='flag-5'>linux</b>內(nèi)核中的<b class='flag-5'>debugfs</b>該怎樣去使用呢?

    Linux驅(qū)動函數(shù)接口說明

    函數(shù)接口說明 創(chuàng)建目錄、文件函數(shù): /* 創(chuàng)建目錄 */ struct dentry *debugfs_create_dir( const char *name, struct dentry
    的頭像 發(fā)表于 09-27 11:20 ?377次閱讀