当前位置: 首页 > ds >正文

linux 内核 debugfs 使用介绍

一:概述

        debugfs 是 Linux 内核提供的一个特殊的虚拟文件系统,用于 暴露内核模块(如驱动)内部的调试信息或控制接口,供开发者、调试人员实时查看和排查问题。即 debugfs 就是一个“调试专用的 /proc 或 /sys”,方便你在不重启或不修改代码的情况下,查看内核模块的运行状态、统计信息,甚至直接向内核模块传递调试指令。

二:挂着位置和查看方式

        1. 默认挂载在 /sys/kernel/debug
        2. 如果没有挂着,可以手动挂载  sudo mount -t debugfs none /sys/kernel/debug


        3. 常见的挂载节点有:

三: 示例

        创建一个内核模块,挂载 debugfs,暴露一个文件 /sys/kernel/debug/hello_debugfs,可以用 cat 来读取内容,用 echo 向它写入数据。

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/debugfs.h>
#include <linux/uaccess.h>#define BUF_SIZE 128static struct dentry *dir_entry;
static struct dentry *file_entry;static char message[BUF_SIZE] = "Hello, debugfs!\n";// 读操作
static ssize_t hello_read(struct file *filp, char __user *buffer,size_t len, loff_t *offset)
{return simple_read_from_buffer(buffer, len, offset, message, strlen(message));
}// 写操作
static ssize_t hello_write(struct file *filp, const char __user *buffer,size_t len, loff_t *offset)
{if (len >= BUF_SIZE)return -EINVAL;if (copy_from_user(message, buffer, len))return -EFAULT;message[len] = '\0';return len;
}static const struct file_operations hello_fops = {.owner = THIS_MODULE,.read = hello_read,.write = hello_write,
};static int __init hello_debugfs_init(void)
{dir_entry = debugfs_create_dir("hello_dir", NULL);if (!dir_entry) {pr_err("Failed to create debugfs directory\n");return -ENOMEM;}file_entry = debugfs_create_file("hello_debugfs", 0666, dir_entry, NULL, &hello_fops);if (!file_entry) {pr_err("Failed to create debugfs file\n");debugfs_remove(dir_entry);return -ENOMEM;}pr_info("hello_debugfs module loaded\n");return 0;
}static void __exit hello_debugfs_exit(void)
{debugfs_remove_recursive(dir_entry);pr_info("hello_debugfs module unloaded\n");
}module_init(hello_debugfs_init);
module_exit(hello_debugfs_exit);MODULE_LICENSE("GPL");
MODULE_AUTHOR("OpenAI ChatGPT");
MODULE_DESCRIPTION("Example using debugfs");

http://www.xdnf.cn/news/3211.html

相关文章:

  • Python 打包兼容Win7 的Qt 程序
  • 【题解-Acwing】869. 试除法求约数
  • 解决react-native下背景图渲染,统一处理组件BackgroundImage
  • 【Python笔记 05】 if判断、比较运算符与逻辑运算符
  • 《AI大模型应知应会100篇》【精华】第40篇:长文本处理技巧:克服大模型的上下文长度限制
  • 如何防止丝杆支撑座锈蚀?
  • MIT6.S081-lab7
  • 第12讲:组合多图(Patchwork)艺术
  • C++复习补充 IO
  • Nginx核心功能与LNMP部署
  • C语言Makefile编写与使用指南
  • 小米喷墨打印机Mi All-in-One Inkjet Printer电脑通过管理打印设备扫描文件方法完整记录
  • 「国产嵌入式仿真平台:高精度虚实融合如何终结Proteus时代?」——从教学实验到低空经济,揭秘新一代AI赋能的产业级教学工具
  • 使用O_DIRECT + 批量写数据到磁盘对丢包率的优化
  • Hanko:身份验证和用户管理解决方案,Clerk Auth0 的开源替代
  • [密码学实战]SDF之对称运算类函数(四)
  • 【缓冲区分析】叠加分析-要素叠加
  • Plesk 下的 IP 地址管理
  • MicroBlaze软核的开发使用
  • 分步详解:凤凰6000模拟器接入Unity Input System‌(
  • docker排查OOM Killer
  • SVN子路径权限设置及登录方法详解
  • docker学习笔记6-安装wordpress
  • AB3 有效括号序列
  • C++的vector中emplace_back() 与 push_back() 的区别
  • 新型电子式EDT-5土动三轴实验系统
  • NodeJS读写(同步异步、流式、分片策略)
  • CentOS环境下搭建seata(二进制、MySQL)
  • 安装deepspeed时出现了以下的错误,如何解决CUDA_HOME does not exist
  • vue3+flex动态的绘制蛇形时间轴