×ðÁú¿­Ê±¹ÙÍøµÇ¼

LinuxÇý¶¯ | debugfs½Ó¿Ú½¨Éè

µã»÷ÉÏ·½¡°Ç¶ÈëʽLinux³äµçÕ¾¡±£¬Ñ¡Ôñ¡°Öö¥/ÐDZêÃñÖںš±

¸£Àû¸É»õ£¬µÚһʱ¼äËÍ´ï

ÉÏƪÏÈÈÝÁËprocfs½Ó¿ÚµÄ½¨É裬½ñÌìÔÙÏÈÈÝÒ»ÖÖdebugfs½Ó¿ÚµÄ½¨Éè¡£

ʵÏÖЧ¹û

ÔÚ/sys/kernel/debug/Ŀ¼Ï½¨ÉèÒ»¸öion/testÎļþ£¬Í¨¹ýcat¡¢echoµÄ·½·¨¾ÙÐжÁд²Ù×÷£º

Ç°ÆÚ×¼±¸

ÄÚºËÉèÖ÷­¿ª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 nulln");
        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] = "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");

    //½¨ÉèÒ»¸ö/sys/kernel/debug/ionĿ¼
    ion_dir = debugfs_create_dir("ion", NULL);
    if (!ion_dir) {
        printk("ion_dir is nulln");
        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 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");

µÇ¼ºó¸´ÖÆ

ÔËÐÐЧ¹û£º

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½Ó¿Ú½¨ÉèµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ