Linux下的Debugfs

debugfs


1. 简介

类似sysfs、procfsdebugfs 也是一种内存文件系统。不过不同于sysfs一个kobject对应一个文件,procfs和进程相关的特性,debugfs的灵活度很大,可以根据需求对指定的变量进行导出并提供读写接口。debugfs又是一个Linux中 everything is a file 哲学的体现,通过VFS实现了对驱动的控制。可以通过以下命令,来挂载debugfs到指定目录。
Debugfs其存在的主要意义是为了内核开发者向用户空间传递更多有用的信息,与proc不同,proc只提供进程相关的信息;同时也与sysfs不同,sysfs对每个文件都要求一定的规则,而Debugfs没有任何的规则。
简而言之,Debugfs是一种用于内核调试的虚拟文件系统。
用途:将内核程序中的变量以文件的形式直观的展现出来,并可以直接通过文件操作来读取或修改内核变量的值,便于开发调试

2. 食用方式

1. 内核使能Debudfs

menuconfig中使能DEBUG_FS = y

在这里插入图片描述

2.挂载命令

mount -t debugfs none /sys/kernel/debug

3.内核接口

1. API接口

  • 想要使用Debugfs功能,首先要做的就是要包含 <linux/debugfs.h>头文件
  • 使用debugfs_create_dir接口,创建一个文件夹,用于保存debugfs所操作的文件
  • 使用debugfs_create_file接口,创建多个文件进行操作

2. API介绍

debugfs_create_dir

struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);
参数作用备注
name文件夹名称
parent父目录如果为NULL,则在root根目录下

debugfs_create_file

struct dentry *debugfs_create_file(const char *name, umode_t mode,struct dentry *parent, void *data,const struct file_operations *fops);
参数作用备注
name文件夹名称
mode文件访问权限可以是整型(比如0644)也可以使用内核的宏
parent父目录,用于保存该文件如果为空则该文件的父目录为根目录
data文件操作的私有数据inode的i_private字段指向这个结构
fops文件的自定义操作可以自定义所有操作接口,也可以使用宏DEFINE_SIMPLE_ATTRIBUTE指定读写操作即可

DEFINE_SIMPLE_ATTRIBUTE宏

// get : 读操作
// set : 写操作
// fmt : 用于指定 get 和 set 函数的输入输出格式。
//例如,它可以定义一个整数、浮点数或字符串的格式,以便在 sysfs 接口中正确显示或解析值。
#define DEFINE_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt)		\
static int __fops ## _open(struct inode *inode, struct file *file)	\
{									\__simple_attr_check_format(__fmt, 0ull);			\return simple_attr_open(inode, file, __get, __set, __fmt);	\
}									\
static const struct file_operations __fops = {				\.owner	 = THIS_MODULE,						\.open	 = __fops ## _open,					\.release = simple_attr_release,					\.read	 = simple_attr_read,					\.write	 = simple_attr_write,					\.llseek	 = generic_file_llseek,					\
}

debugfs_create_u8

void debugfs_create_u8(const char *name, umode_t mode,struct dentry *parent, u8 *value);
参数作用备注
name文件名一般以变量名来命名,方便调试
mode文件权限
parent父目录
value变量变量的值会被存在文件内

其他变量类型的接口:👇

//创建十进制的无符号文件
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);//创建一个size_t类型的文件
void debugfs_create_size_t(const char *name, umode_t mode,struct dentry *parent, size_t *value);
//创建一个unsigned long类型的文件
struct dentry *debugfs_create_ulong(const char *name, umode_t mode,struct dentry *parent,unsigned long *value);
//创建一个十六进制的unsigned long类型的文件
void debugfs_create_xul(const char *name, umode_t mode,struct dentry *parent, unsigned long *value);
// 布尔型
void debugfs_create_bool(const char *name, umode_t mode,struct dentry *parent, bool *value);

Deme one

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/debugfs.h>static struct dentry *dir = NULL;static unsigned int debugfs_hello;static u32 sum = 0;static int add_write(void *data, u64 value)
{sum += value;return 0;
}
// 文件操作变量名,get读操作,set写操作,将读写操作的返回值以字符串格式化的形式读出或写入文件内部
DEFINE_SIMPLE_ATTRIBUTE(add_ops, NULL, add_write, "%llu\n");static __init int hello_init(void)
{struct dentry *tmp_dir = NULL;/* create /sys/kernel/debug/debugfs_hello/ directory */dir = debugfs_create_dir("debugfs_hello", NULL);if (!dir) {printk(KERN_ALERT "debugfs_create_dir failed\n");return -1;}/* create /sys/kernel/debug/debugfs_hello/hello value, mode: rw*/tmp_dir = debugfs_create_u32("hello", 00666, dir, &debugfs_hello);if (!tmp_dir) {printk(KERN_ALERT "debugfs_create_u32 failed\n");return -1;}/* create /sys/kernel/debug/debugfs_hello/add value, mode: w*/tmp_dir = debugfs_create_file("add", 0222, dir, NULL, &add_ops);if (!tmp_dir) {printk(KERN_ALERT "debugfs_create_file failed\n");return -1;}/* create /sys/kernel/debug/debugfs_hello/sum value, mode: r*/tmp_dir = debugfs_create_u32("sum", 0444, dir, &sum);if (!tmp_dir) {printk(KERN_ALERT "debugfs_create_u32 failed\n");return -1;}return 0;
}static void __exit hello_exit(void)
{printk(KERN_INFO "Exit debugfs_hello module\n");debugfs_remove_recursive(dir);dir = NULL;
}module_init(hello_init);
module_exit(hello_exit);MODULE_LICENSE("GPL");

Demo TWO

#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 zen_buf[512] = "hello\n";
static struct dentry *zen_dir;static int zen_open(struct inode *inode, struct file *filp)
{printk("zen open\n");filp->private_data = inode->i_private;return 0;
}ssize_t zen_read(struct file *filp, char __user *buf, size_t count, loff_t *offp)
{int retval = 0;if ((*offp + count) > 512)count = 512 - *offp;printk("read request: count:%u, offset:%u\n", count, *offp);if (copy_to_user(buf, zen_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 zen_write(struct file *filp, const char __user *buff, size_t count, loff_t *offp)
{int retval;printk("write request: count:%u, offset:%u\n", count, *offp);if (*offp > 512)return 0;if (*offp + count > 512)count = 512 - *offp;if (copy_from_user(zen_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 = zen_read,.write = zen_write,.open = zen_open,
};static int __init debugfs_init(void)
{printk("INIT MODULE\n");zen_dir = debugfs_create_dir("zen_dir4", NULL);if (!zen_dir) {printk("zen_dir4 is null\n");goto fail;}static struct dentry *sub_zen_dir;sub_zen_dir = debugfs_create_dir("sub_zen", zen_dir);if (!sub_zen_dir) {printk("sub zen dir is null\n");goto fail;}struct dentry *filent = debugfs_create_file("zen", 0644, sub_zen_dir, NULL, &my_fops);if (!filent) {printk("zen file is null\n");goto fail;}printk("INIT SUCCESS\n");return 0;
fail:
//return -ENOENT;return -1;
}static void __exit debugfs_exit(void)
{printk("exit module\n");debugfs_remove_recursive(zen_dir);printk("exit success\n");
}module_init(debugfs_init);
module_exit(debugfs_exit);
MODULE_LICENSE("GPL");

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/59120.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

go 包管理

Go语言所依赖的所有的第三方库都放在GOPATH目录下面 gomodule是Go语言默认的依赖管理工具 Modules是相关Go包的集合&#xff0c;是源代码交换和版本控制的单元&#xff0c;用于指定使用哪些源文件 GO111MODULEoff禁用gomodule&#xff0c;编译时从GOPATH和vendor文件夹中查找包…

Fooocus图像生成软件本地部署教程:在Windows上快速上手AI创作

文章目录 前言1. 本地部署Fooocus图像生成软件1.1 安装方式1.2 功能介绍 2. 公网远程访问Fooocus3. 固定Fooocus公网地址 前言 本篇文章将介绍如何在本地Windows11电脑部署开源AI生图软件Fooocus&#xff0c;并结合Cpolar内网穿透工具轻松实现公网环境远程访问与使用。 Foooc…

Linux高阶——1103——Signal信号机制

1、信号机制 在linux和unix系统下&#xff0c;如果想要处置(挂起&#xff0c;结束)进程&#xff0c;可以使用信号&#xff0c;经典消息机制&#xff0c;所以进程包括系统进程都是利用信号处置进程的 kill -l——查看所有系统支持的信号 1-31号信号——Unix经典信号&#xff…

修改HarmonyOS鸿蒙图标和名字,打包后安装到真机,应用图标丢失变成透明,修改名字也不生效,还是默认的labeL解决方案教程

HarmonyOS鸿蒙打包hap 安装应用到桌面没有图标&#xff0c;用hdc安装到真机&#xff0c;打包后应用图标丢失变成透明&#xff0c;名字也还是默认的label的bug&#xff0c;以下是解决方案 以下是修改方案&#xff1a; 1、修改应用名字&#xff1a; 2、修改应用图标&#xff1a…

Python小游戏20——超级玛丽

首先&#xff0c;你需要确保你的Python环境中安装了pygame库。如果还没有安装&#xff0c;可以使用以下命令进行安装&#xff1a; bash pip install pygame 运行效果展示 代码展示 python import pygame import sys # 初始化pygame pygame.init() # 设置屏幕尺寸 screen_width …

CTF-WEB: python模板注入

漏洞是如何产生的&#xff1f; Python模板注入漏洞通常出现在使用模板引擎生成动态内容的应用中。如果用户输入没有经过适当的处理直接插入模板中&#xff0c;就可能会导致模板注入漏洞。一个常见的例子是使用Jinja2模板引擎时&#xff0c;如果直接渲染用户输入&#xff0c;则…

从富文本窥探苹果的代码秘密

从富文本窥探苹果的代码秘密 背景 在我们的业务场景下&#xff0c;为突出诸如 “利益点”和“利率” 等特性以推动订单成交&#xff0c;引入了 “富文本” 这一概念。富文本具备丰富格式的文本展示与编辑功能。然而&#xff0c;恰是由于富文本具有 “多样式”“复杂排版” 等特…

spark集群模式-standalone的配置和使用

其中所需要的python和spark的软件在该文章中 spark-本地模式的配置和简单使用-CSDN博客 环境配置 首先在别的服务器上均要进行python环境的配置 安装spark 1、上传&#xff0c;解压&#xff0c;重命名 tar -zxvf /opt/install/spark-3.1.2-bin-hadoop3.2.tgz -C /opt/modu…

openstack之guardian介绍与实例创建过程

运行特征 采集模块&#xff1a;扩展Ceilometer&#xff0c;采集存储网、业务网连通性、nova目录是否可读写&#xff1b; 收集模块&#xff1a;将采集到的数据存储到数据库中&#xff1b; 分析模块&#xff1a;根据采集的结果&#xff0c;分析各节点状态&#xff0c;并进行反向检…

AVLTree

1.AVL树的概念 二叉搜索树虽然可以提高查找的效率&#xff0c;但是如果数据有序或者接近有序&#xff0c;二叉搜索树将退化为单支树&#xff0c;查找元素相当于在顺序表中搜索元素&#xff0c;效率低下。为了解决该问题&#xff0c;于是就有了AVLTree。即当向二叉搜索树中插入…

使用 async/await 时未捕获异常的问题及解决方案

使用 async/await 时未捕获异常的问题及解决方案 1. 引言 在现代 JavaScript 开发中&#xff0c;async/await 是处理异步操作的强大工具&#xff0c;它使得异步代码看起来更像同步代码&#xff0c;提升了代码的可读性和可维护性。然而&#xff0c;在使用 async/await 时&…

【数据结构二叉树】C非递归算法实现二叉树的先序、中序、后序遍历

引言: 遍历二叉树&#xff1a;指按某条搜索路径巡访二叉树中每个结点&#xff0c;使得每个结点均被访问一次&#xff0c;而且仅被访问一次。 除了层次遍历外&#xff0c;二叉树有三个重要的遍历方法&#xff1a;先序遍历、中序遍历、后序遍历。 1、递归算法实现先序、中序、后…

深入学习 Scrapy 框架:从入门到精通的全面指南

深入学习 Scrapy 框架&#xff1a;从入门到精通的全面指南 引言 在数据驱动的时代&#xff0c;网络爬虫成为了获取信息的重要工具。Scrapy 是一个强大的 Python 爬虫框架&#xff0c;专为快速高效地提取网页数据而设计。本文将深入探讨 Scrapy 的使用&#xff0c;从基础知识到…

蓝桥杯 区间移位--二分、枚举

题目 代码 #include <stdio.h> #include <string.h> #include <vector> #include <algorithm> #include <iostream> using namespace std; struct node{ int a,b; }; vector<node> q; bool cmp(node x,node y){ return x.b <…

SpringBoot+VUE2完成WebSocket聊天(数据入库)

下载依赖 <!-- websocket --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-websocket</artifactId></dependency><!-- MybatisPlus --><dependency><groupId>com.ba…

图文深入介绍Oracle DB link(一)

1. 引言&#xff1a; 本文图文深入介绍Oracle DB link&#xff0c;先介绍基本概念。 2.DB link的定义 数据库链接&#xff08;Database Link&#xff0c;简称 DB Link&#xff09;是 Oracle 数据库中的一个重要功能。它是一种在一个 Oracle 数据库实例中访问另一个 Oracle 数…

MoonBit 双周报 Vol.59:新增编译器常量支持,改进未使用警告,支持跨包函数导入...多个关键技术持续优化中!

2024-11-04 MoonBit更新 增加了编译期常量的支持。常量的名字以大写字母开头&#xff0c;用语法 const C ... 声明。常量的类型必须是内建的数字类型或 String。常量可以当作普通的值使用&#xff0c;也可以用于模式匹配。常量的值目前只能是字面量&#xff1a; const MIN_…

HTB:Shocker[WriteUP]

目录 连接至HTB服务器并启动靶机 1.How many TCP ports are listening on Shocker? 使用nmap对靶机TCP端口进行开放扫描 2.What is the name of the directory available on the webserver that is a standard name known for running scripts via the Common Gateway Int…

【算法赌场】SPFA算法

1.算法简介 SPFA 算法的全称是&#xff1a;Shortest Path Faster Algorithm&#xff0c;是西南交通大学 段凡丁 于 1994 年发表的论文中的名字。不过&#xff0c;段凡丁的证明是错误的&#xff0c;且在 Bellman-Ford 算法提出后不久&#xff08;1957 年&#xff09;已有队列优化…

YOLOv11改进策略【卷积层】| CGblock 内容引导网络 利用不同层次信息,提高多类别分类能力 (含二次创新)

一、本文介绍 本文记录的是利用CGNet中的CG block模块优化YOLOv11的目标检测网络模型。CG block通过局部特征提取器、周围环境提取器、联合特征提取器和全局环境提取器来提取局部特征、周围环境和全局环境信息,充分利用不同层次的信息。本文将其应用到v11中,并进行二次创新,…