Linux驱动1_DebugFS

简介:
//	DebugFS: 
//		默认情况下,debugfs会被挂载在目录/sys/kernel/debug
//		手动挂载,mount -t debugfs none /your/debugfs/dir

//	创建目录/文件:
struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);
struct dentry *debugfs_create_file(const char *name, mode_t mode, 
        struct dentry *parent, void *data, 
        const struct file_operations *fops);

//	删除目录/文件
void debugfs_remove(struct dentry *dentry);
//	删除目录下所有dentry
void debugfs_remove_recursive(struct dentry *dentry);


//	创建单值对象:
struct dentry *debugfs_create_u8(const char *name, mode_t mode, 
        struct dentry *parent, u8 *value);
struct dentry *debugfs_create_u16(const char *name, mode_t mode, 
        struct dentry *parent, u16 *value);
struct dentry *debugfs_create_u32(const char *name, mode_t mode, 
        struct dentry *parent, u32 *value);
struct dentry *debugfs_create_u64(const char *name, mode_t mode, 
        struct dentry *parent, u64 *value);
 
struct dentry *debugfs_create_x8(const char *name, mode_t mode, 
        struct dentry *parent, u8 *value);
struct dentry *debugfs_create_x16(const char *name, mode_t mode, 
        struct dentry *parent, u16 *value);
struct dentry *debugfs_create_x32(const char *name, mode_t mode, 
        struct dentry *parent, u32 *value);
 
struct dentry *debugfs_create_size_t(const char *name, mode_t mode, 
        struct dentry *parent, size_t *value);
struct dentry *debugfs_create_bool(const char *name, mode_t mode, 
        struct dentry *parent, u32 *value);


//	创建二进制对象:
struct debugfs_blob_wrapper {
    void *data;
    unsigned long size;
};
struct dentry *debugfs_create_blob(const char *name, mode_t mode, 
         struct dentry *parent, struct debugfs_blob_wrapper *blob);


//	其他:
struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry, 
        struct dentry *new_dir, const char *new_name);
 
struct dentry *debugfs_create_symlink(const char *name, 
        struct dentry *parent, const char *target);

//	简单封装debugfs,实现对内核缓存区的读访问,方便调试驱动观察缓存区:
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/debugfs.h> 
struct debug_struct
{
	void *buffer;
	int buffer_len;
	struct dentry *file;
};
static int debug_open(struct inode *inode, struct file *filp)
{
    filp->private_data = inode->i_private;
    return 0;
}
static ssize_t debug_read(struct file *fp, char __user *user_buffer, 
                                size_t count, loff_t *position) 
{ 
	struct debug_struct *s = (struct debug_struct *)fp->private_data;
    return simple_read_from_buffer(user_buffer, count, position, s->buffer, s->buffer_len);
}  
const struct file_operations debug_fops = { 
        .read = debug_read, 
        .open = debug_open, 
}; 


static int debug_create_file(struct dentry *dir, struct debug_struct *st, const char *name)
{
	if(!st)
		return -EINVAL;
	st->file = debugfs_create_file(name, 0644, dir, st, &debug_fops);
	if(!st->file)
		return -ENODEV;
	return 0;
}
static int debug_remove_file(struct debug_struct *st)
{
	if(!st)
		return -EINVAL;
	debugfs_remove(st->file);
	st->file = NULL;
	return 0;
}


//	测试:
//		1.创建核心内存mem
//		2.通过debugfs,创建mem的文件
#define BUFFER_SIZE	1024
struct debug_struct *st;
struct dentry *dir;
static void debug_init(void)
{
	int ret = 0;
	st = (struct debug_struct *)kmalloc(sizeof(struct debug_struct), GFP_KERNEL);
	if(!st)
	{
		printk(KERN_ALERT "debug test: kmalloc debug_struct failed.\n");
		goto err_kmalloc_st;
	}
	st->buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
	if(!st->buffer)
	{
		printk(KERN_ALERT "debug test: kmalloc buffer failed.\n");
		goto err_kmalloc_buff;
	}
	st->buffer_len = BUFFER_SIZE;

	dir = debugfs_create_dir("debug_dir", NULL);
	if(!dir)
	{
		printk(KERN_ALERT "debug test: debugfs_create_dir failed.\n");
		goto err_create_dir;
	}

	ret = debug_create_file(dir, st, "debug_mem");
	if(ret)
	{
		printk(KERN_ALERT "debug test: debug_create_file failed.\n");
		goto err_create_file;
	}
	//now you can see debug_dir in /sys/kernel/debug/
	return;


err_create_file:
	debugfs_remove_recursive(dir);
err_create_dir:
	kfree((void *)st->buffer);
err_kmalloc_buff:
	kfree((void *)st);
err_kmalloc_st:
	return;
}	


static void debug_uinit(void)
{
	int ret = 0;
	debug_remove_file(st);
	debugfs_remove_recursive(dir);
	kfree((void *)st->buffer);
	kfree((void *)st);
	return;
}

//	参考:
//		http://www.linuxforu.com/2010/10/debugging-linux-kernel-with-debugfs/
//		http://www.cnblogs.com/wwang/archive/2011/01/17/1937609.html



目录
相关文章
|
21天前
|
Java Linux API
Linux设备驱动开发详解2
Linux设备驱动开发详解
22 6
|
21天前
|
消息中间件 算法 Unix
Linux设备驱动开发详解1
Linux设备驱动开发详解
23 5
|
22天前
|
Ubuntu NoSQL Linux
Linux内核和驱动
Linux内核和驱动
16 2
|
29天前
|
数据采集 Linux
Linux源码阅读笔记20-PCI设备驱动详解
Linux源码阅读笔记20-PCI设备驱动详解
|
14天前
|
Linux API
Linux里的高精度时间计时器(HPET)驱动 【ChatGPT】
Linux里的高精度时间计时器(HPET)驱动 【ChatGPT】
|
2月前
|
存储 JSON Linux
|
2月前
|
Oracle 关系型数据库 Linux
讲解linux下的Qt如何编译oracle的驱动库libqsqloci.so
通过这一连串的步骤,可以专业且有效地在Linux下为Qt编译Oracle驱动库 `libqsqloci.so`,使得Qt应用能够通过OCI与Oracle数据库进行交互。这些步骤适用于具备一定Linux和Qt经验的开发者,并且能够为需要使用Qt开发数据库应用的专业人士提供指导。
77 1
讲解linux下的Qt如何编译oracle的驱动库libqsqloci.so
|
27天前
|
Linux
【linux】【驱动】<specifier>-map-pass-thru讲解
【linux】【驱动】<specifier>-map-pass-thru讲解
12 0
|
27天前
|
Linux
【linux】【驱动】phy接口类型
【linux】【驱动】phy接口类型
12 0
|
2月前
|
缓存 网络协议 算法
【Linux系统编程】深入剖析:四大IO模型机制与应用(阻塞、非阻塞、多路复用、信号驱动IO 全解读)
在Linux环境下,主要存在四种IO模型,它们分别是阻塞IO(Blocking IO)、非阻塞IO(Non-blocking IO)、IO多路复用(I/O Multiplexing)和异步IO(Asynchronous IO)。下面我将逐一介绍这些模型的定义:
118 1