WPS?2019如何清理云空間?WPS 2019如何將PDF轉(zhuǎn)換為WORD?
WPS 2019如何清理云空間?進(jìn)入金山文檔網(wǎng)頁(yè)端,點(diǎn)擊我的文檔勾
2023/04/10
上篇介紹了procfs接口的創(chuàng)建,今天再介紹一種debugfs接口的創(chuàng)建。
(資料圖片)
在/sys/kernel/debug/
目錄下創(chuàng)建一個(gè)ion/test
文件,通過(guò)cat
、echo
的方式進(jìn)行讀寫操作:
內(nèi)核配置打開(kāi)debugfs:
CONFIG_DEBUG_FS=y
掛載debugfs文件系統(tǒng):
mount -t debugfs none /sys/kernel/debug
#include #include #include static struct dentry *ion_dir;static u64 test_u64 = 0;static int __init debugfs_init(void){ //創(chuàng)建一個(gè)/sys/kernel/debug/ion目錄 ion_dir = debugfs_create_dir("ion", NULL); if (!ion_dir) { printk("ion_dir is null\\n"); 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");
運(yùn)行結(jié)果:
#include #include #include #include #include #include #include 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"); //創(chuàng)建一個(gè)/sys/kernel/debug/ion目錄 ion_dir = debugfs_create_dir("ion", NULL); if (!ion_dir) { printk("ion_dir is null\\n"); 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 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");
運(yùn)行結(jié)果:
創(chuàng)建目錄、文件函數(shù):
/* 創(chuàng)建目錄 */struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);/*創(chuàng)建節(jié)點(diǎn) */struct dentry *debugfs_create_file(const char *name, umode_t mode, struct dentry *parent, void *data, const struct file_operations *fops);
name:要?jiǎng)?chuàng)建的/sys/kernel/debug
下的目錄名
parent:父目錄,用struct dentry
結(jié)構(gòu)體表示。 如果直接在/sys/kernel/debug/
下創(chuàng)建文件,則為NULL
創(chuàng)建不同大小的文件:
//創(chuàng)建十進(jìn)制的無(wú)符號(hào)文件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);//創(chuàng)建十六進(jìn)制的無(wú)符號(hào)文件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);
更詳細(xì)的debugfs用法請(qǐng)參考官方文檔:Documentation/filesystems/debugfs.txt
標(biāo)簽: