Linux内核基础篇——动态输出调试

简介: Linux内核基础篇——动态输出调试

上篇说到printk调试,但printk是全局的,只能设置输出等级。而动态输出可以动态选择打开某个内核子系统的输出,可以有选择性地打开某些模块的输出。

配置内核编译选项

要使用动态输出,必须在配置内核时打开CONFIG_DYNAMIC_DEBUG宏。内核代码里使用大量pr_debug()/dev_dbg()函数来输出信息,这些就使用了动态输出。

需要打开的内核配置选项:

CONFIG_DEBUG_FS=y
CONFIG_DYNAMIC_DEBUG=y

CONFIG_DYNAMIC_DEBUG是配置动态输出,它依赖于CONFIG_DEBUG_FS,而CONFIG_DEBUG_FSdebugfs文件系统

打开内核配置后,我们还需要挂载debugfs文件系统

debugfs文件系统挂载

动态输出在debugfs文件系统中有一个control文件节点,这个文件节点记录了系统中所有使用动态输出技术的文件名路径、输出所在的行号、模块名字和要输出的语句

debugfs默认会挂载到/sys/kernel/debug,如果没有挂载,可以执行以下命令挂载:

# mount -t debugfs none /sys/kernel/debug/

挂载debugfs文件系统后,可以查看control节点内容:

# cat /sys/kernel/debug/dynamic_debug/control

动态输出使用

打开svcsock.c文件中所有的动态输出语句

# echo 'file svcsock.c +p' > /sys/kernel/debug/dynamic_debug/control

打开usbcore模块中所有的动态输出语句

# echo 'module usbcore +p' > /sys/kernel/debug/dynamic_debug/control

打开svc_process()函数中所有的动态输出语句

# echo 'func svc_process() +p' > /sys/kernel/debug/dynamic_debug/control

打开文件路径包含usb的文件里所有的动态输出语句

# echo -n '*usb* +p' > /sys/kernel/debug/dynamic_debug/control

打开系统所有的动态输出语句

# echo -n '+p' > /sys/kernel/debug/dynamic_debug/control

上面是打开动态输出语句的例子,除了能输出pr_debug()/dev_dbg()函数中定义的输出信息外,还能输出一些额外信息,如函数名、行号、模块名字以及线程ID等

  • p:打开动态输出语句
  • f:输出函数名
  • l:输出行号
  • m:输出模块名字
  • t:输出线程ID

另外,还可以在各个子系统的Makefile中添加ccflags来打开动态输出语句

<../Makefile>
ccflags-y += -DDEBUG
ccflags-y += -DVERBOSE_DEBUG

实际案例

例如在一个led驱动中的open()、write()等函数开头添加一句pr_debug("%s enter\n", __func__);

#include <linux/module.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
static int major = 0;
static char kernel_buf[1024];
static struct class *hello_class;
#define MIN(a, b) (a < b ? a : b)
static ssize_t hello_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{
 int err;
 pr_debug("%s enter\n", __func__);
 err = copy_to_user(buf, kernel_buf, MIN(1024, size));
 return MIN(1024, size);
}
static ssize_t hello_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
 int err;
 pr_debug("%s enter\n", __func__);
 err = copy_from_user(kernel_buf, buf, MIN(1024, size));
 return MIN(1024, size);
}
static int hello_drv_open (struct inode *node, struct file *file)
{
 pr_debug("%s enter\n", __func__);
 return 0;
}
static int hello_drv_close (struct inode *node, struct file *file)
{
 pr_debug("%s enter\n", __func__);
 return 0;
}
/* 2. 定义自己的file_operations结构体                                              */
static struct file_operations hello_drv = {
 .owner  = THIS_MODULE,
 .open    = hello_drv_open,
 .read    = hello_drv_read,
 .write   = hello_drv_write,
 .release = hello_drv_close,
};
static int __init hello_init(void)
{
 int err;
 pr_debug("%s enter\n", __func__);
 major = register_chrdev(0, "hello", &hello_drv);  /* /dev/hello */
 hello_class = class_create(THIS_MODULE, "hello_class");
 err = PTR_ERR(hello_class);
 if (IS_ERR(hello_class)) {
  unregister_chrdev(major, "hello");
  return -1;
 }
 device_create(hello_class, NULL, MKDEV(major, 0), NULL, "hello"); /* /dev/hello */
 return 0;
}
static void __exit hello_exit(void)
{
 pr_debug("%s enter\n", __func__);
 device_destroy(hello_class, MKDEV(major, 0));
 class_destroy(hello_class);
 unregister_chrdev(major, "hello");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");

为了方面查看,先清除内核输出:

# dmesg -c

然后加载驱动,执行dmesg查看是否有打印:

# insmod hello_drv.ko
# dmesg

此时没有pr_debug()的打印。这时再使用动态输出打开hello_drv模块的动态输出:

# echo 'module hello_drv +p' > /sys/kernel/debug/dynamic_debug/control

然后执行该驱动的应用层程序,使其调用到驱动的open、write、close函数,从而执行pr_debug():

# ./hello_drv_test -w 10

再查看demsg内容:

可以看到,当打开了hello_drv模块的动态输出后,驱动中的pr_debug()语句就可以正常打印了。

再看看debugfs的control节点:

# cat /sys/kernel/debug/dynamic_debug/control

control节点记录了刚刚执行pr_debug()时的文件名、所在行号、模块名、函数名和输出语句(p表示动态输出的语句)。

end

猜你喜欢

Linux内核基础篇——printk调试

Linux内核基础篇——initcall

RISC-V SiFive U64内核——HPM硬件性能监视器

RISC-V SiFive U64内核——L2 Prefetcher预取器

RISC-V SiFive U54内核——PMP物理内存保护

RISC-V SiFive U54内核——PLIC平台级中断控制器

RISC-V SiFive U54内核——CLINT中断控制器

RISC-V SiFive U54内核——中断和异常详解

实战 | RISC-V Linux入口地址2M预留内存优化

RISC-V Linux启动之页表创建分析

RISC-V Linux汇编启动过程分析

RISC-V 入门笔记(新手必看!)

教你在QEMU上运行RISC-V Linux

OpenSBI三种固件的区别

写给新手的MMU工作原理

相关文章
|
8月前
|
安全 网络协议 Linux
深入理解Linux内核模块:加载机制、参数传递与实战开发
本文深入解析了Linux内核模块的加载机制、参数传递方式及实战开发技巧。内容涵盖模块基础概念、加载与卸载流程、生命周期管理、参数配置方法,并通过“Hello World”模块和字符设备驱动实例,带领读者逐步掌握模块开发技能。同时,介绍了调试手段、常见问题排查、开发规范及高级特性,如内核线程、模块间通信与性能优化策略。适合希望深入理解Linux内核机制、提升系统编程能力的技术人员阅读与实践。
731 1
|
8月前
|
Ubuntu Linux
Ubuntu 23.04 用上 Linux 6.2 内核,预计下放到 22.04 LTS 版本
Linux 6.2 带来了多项内容更新,修复了 AMD 锐龙处理器设备在启用 fTPM 后的运行卡顿问题,还增强了文件系统。
|
8月前
|
Ubuntu Linux
Ubuntu 23.10 现在由Linux内核6.3提供支持
如果你想在你的个人电脑上测试一下Ubuntu 23.10的最新开发快照,你可以从官方下载服务器下载最新的每日构建ISO。然而,请记住,这是一个预发布版本,所以不要在生产机器上使用或安装它。
|
8月前
|
传感器 监控 Ubuntu
10 月发布,Ubuntu 23.10 已升级到 Linux Kernel 6.3 内核
硬件方面,Linux 6.3 引入了在 HID 中引入了原生的 Steam Deck 控制器接口,允许罗技 G923 Xbox 版赛车方向盘在 Linux 上运行;改善 8BitDo Pro 2 有线控制器的行为;并为一系列华硕 Ryzen 主板添加传感器监控。
|
8月前
|
Ubuntu Linux
Ubuntu24.04LTS默认采用Linux 6.8内核,实验性版本可通过PPA获得
IT之家提醒,当下的 Ubuntu 23.10 也是一个“短期支持版本”,该版本将在今年 7 月终止支持,而今年 4 月推出的 Ubuntu 24.04 LTS 长期支持版本将获得 5 年的更新支持。
|
8月前
|
监控 Ubuntu Linux
什么Linux,Linux内核及Linux操作系统
上面只是简单的介绍了一下Linux操作系统的几个核心组件,其实Linux的整体架构要复杂的多。单纯从Linux内核的角度,它要管理CPU、内存、网卡、硬盘和输入输出等设备,因此内核本身分为进程调度,内存管理,虚拟文件系统,网络接口等4个核心子系统。
880 0
|
8月前
|
Web App开发 缓存 Rust
|
8月前
|
Ubuntu 安全 Linux
Ubuntu 发行版更新 Linux 内核,修复 17 个安全漏洞
本地攻击者可以利用上述漏洞,攻击 Ubuntu 22.10、Ubuntu 22.04、Ubuntu 20.04 LTS 发行版,导致拒绝服务(系统崩溃)或执行任意代码。
|
8月前
|
Ubuntu 机器人 物联网
Linux Ubuntu 22.04 LTS 测试版实时内核已可申请
请注意,在启用实时内核后您需要手动配置 grub 以恢复到原始内核。更多内容请参考:

热门文章

最新文章