首页 > 其他分享 >kernel——debugfs

kernel——debugfs

时间:2022-11-26 19:12:27浏览次数:45  
标签:kernel arr dentry debugfs static u32 hello

1. 准备

首先在 menuconfig 时开启 debugfs
Kernel hacking --->
Generic Kernel Debugging Instruments --->
[*] Debug Filesystem
Debugfs default access (Access normal) --->

挂载debugfs

yangxr@vexpress:/ # cat /etc/fstab
proc           /proc      proc    defaults   0     0
tmpfs          /tmp       tmpfs   defaults   0     0
sysfs          /sys       sysfs   defaults   0     0
var            /dev       tmpfs   defaults   0     0
ramfs          /dev       tmpfs   defaults   0     0
debugfs        /debug     debugfs defaults   0     0

2. 示例

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/debugfs.h>
#include <linux/uaccess.h>

static struct dentry *hello_dentry;
static u32 hello_u32;
static u16 hello_u16;
static u32 hello_arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9};

static struct debugfs_u32_array arr_info = {
        .array = hello_arr,
        .n_elements = 9,
};

static struct debugfs_blob_wrapper mem_info;

#define HELLO_MAX_SIZE 32
static char hello_buf[HELLO_MAX_SIZE];

static ssize_t hello_read (struct file *filp, char __user *to,
                size_t count, loff_t *ppos)
{
        return simple_read_from_buffer(to, count, ppos, hello_buf, HELLO_MAX_SIZE);
}

static ssize_t hello_write (struct file *filp, const char __user *from,
                size_t count, loff_t *ppos)
{
        if (*ppos == HELLO_MAX_SIZE)
                return count;
        return simple_write_to_buffer(hello_buf, HELLO_MAX_SIZE, ppos,
                from, count);
}

static struct file_operations hello_fops = {
        .owner = THIS_MODULE,
        .read = hello_read,
        .write = hello_write,
};

static int __init hello_init(void)
{
        if ((hello_dentry = debugfs_create_dir("hello", NULL)) == NULL) {
                printk("Create /debug/hello failed !\n");
                return -1;
        }
        printk("Create /debug/hello succeed !\n");

        // 创建u32节点,使用 10进制显示
        debugfs_create_u32("u32", 0666, hello_dentry, &hello_u32);
        // 创建u16节点,使用 16进制显示
        debugfs_create_x16("u16", 0666, hello_dentry, &hello_u16);
        // 创建u32数组 (只读)
        debugfs_create_u32_array("u32_arr", 0666, hello_dentry, &arr_info);

        // 显示一片内存 (只读)
        mem_info.data = (void *)hello_arr;
        mem_info.size = sizeof(hello_arr);
        debugfs_create_blob("mem", 0666, hello_dentry, &mem_info);

        if (debugfs_create_file("hello_fops", 0666,
                                hello_dentry, hello_buf, &hello_fops) == NULL) {
                printk("Create /debug/hello_fops failed\n");
                return -1;
        }

        return 0;
}

static void __exit hello_exit(void)
{
        if (hello_dentry) {
                // 递归删除/debug/hello
                debugfs_remove_recursive(hello_dentry);
                printk("Remove /debug/hello succeed !\n");
        }
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");

标签:kernel,arr,dentry,debugfs,static,u32,hello
From: https://www.cnblogs.com/yangxinrui/p/16928071.html

相关文章

  • kernel module in UEFI secure boot -
    #insmodlkm_hello.koinsmod:ERROR:couldnotinsertmodulelkm_hello.ko:Operationnotpermitted解决办法其实就是因为修改.ko文件是修改的linux内核文件,所以被bios......
  • kernel——proc
    1.简介proc文件系统:一种和内核交互的接口,最早专用于读写进程信息。特点:proc文件系统的文件节点只能从内核层创建,且这些节点的ops和设备节点类似,是由创建者定义的。......
  • kernel——文件系统
    设备端文件系统的格式所有文件系统都使用如下格式为基础如minix,适用于小容量环境如ext2,适用于大容量环境,于是进行了扩展具体分析设备上的文件系统以最简单的minix......
  • 随想录(在实践中学习kernel代码)
       记得我在读书的时候,虽然老师也教过操作系统的课程,但是自己的理解却不是很充分,实践部分的内容就更少。对于课程中的内容,比如说中断、互斥、线程、IO等概念常常也是一......
  • 【强大的数据迁移和恢复解决方案】KernelApps及其产品介绍
     KernelApps是范围广泛的强大的数据迁移和恢复解决方案可以根据用户的需求提供多种采购方案  KernelApps提供交换服务器、Office365、电子邮件迁移......
  • virt_to_phys on 5.4 kernel
     virt_to_physon5.4kernel5.4\arch\arm64\include\asm\Memory.hstaticinlinephys_addr_tvirt_to_phys(constvolatilevoid*x){return__virt_to_phys(......
  • kernel module in UEFI secure boot --- insmod: ERROR: could not insert module lk
    #insmodlkm_hello.koinsmod:ERROR:couldnotinsertmodulelkm_hello.ko:Operationnotpermitted解决办法其实就是因为修改.ko文件是修改的linux内核文件,所以被bi......
  • Debian编译安装最新的Linux kernel 6.1.0-rc3
    没有编译过​​Linux kernel的Linux运维工作是不完整的安装必要的包sudoaptupdate-y&&sudoaptinstallmakegccbc-y&&sudoaptinstallpkg-configlibncurses-de......
  • kernel——字符设备驱动
    字符设备驱动的框架设备节点:inode,类型为字符设备,记录设备号设备号:内核确定驱动的唯一编号cdev:字符驱动对象框架代码驱动#include<linux/module.h>#include<linux......
  • Using debugStub to debug a guest linux kernel
    UsingdebugStubtodebugaguestlinuxkernelIamrunningVMwareFusionVersion6.0.2(1398658)IhaveconfiguredthefollowinginmyvmxdebugStub.listen.gue......