linux - dynamic debug logging机制
创始人
2024-06-03 17:24:43
0

引用

  • Dynamic debug — The Linux Kernel documentation


一. 什么叫dynamic debug?

Dynamic debug allows you to dynamically enable/disable kernel debug-print code to obtain additional kernel information.

If /proc/dynamic_debug/control exists, your kernel has dynamic debug. You’ll need root access (sudo su) to use this.

Dynamic debug provides:

  • a Catalog of all prdbgs in your kernel. cat /proc/dynamic_debug/control to see them.

  • a Simple query/command language to alter prdbgs by selecting on any combination of 0 or 1 of:

  • source filename

  • function name

  • line number (including ranges of line numbers)

  • module name

  • format string

  • class name (as known/declared by each module)

二. 内核API

pr_debug()
dev_dbg()
print_hex_dump_debug()
print_hex_dump_bytes()
  • printk.h - include/linux/printk.h - Linux source code (v6.2.6) - Bootlin

/* If you are writing a driver, please use dev_dbg instead */
#if defined(CONFIG_DYNAMIC_DEBUG) || \(defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
#include /*** pr_debug - Print a debug-level message conditionally* @fmt: format string* @...: arguments for the format string** This macro expands to dynamic_pr_debug() if CONFIG_DYNAMIC_DEBUG is* set. Otherwise, if DEBUG is defined, it's equivalent to a printk with* KERN_DEBUG loglevel. If DEBUG is not defined it does nothing.** It uses pr_fmt() to generate the format string (dynamic_pr_debug() uses* pr_fmt() internally).*/
#define pr_debug(fmt, ...)            \dynamic_pr_debug(fmt, ##__VA_ARGS__)
#elif defined(DEBUG)
#define pr_debug(fmt, ...) \printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
#else
#define pr_debug(fmt, ...) \no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
#endif
  • dev_printk.h - include/linux/dev_printk.h - Linux source code (v6.2.6) - Bootlin

#if defined(CONFIG_DYNAMIC_DEBUG) || \(defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
#define dev_dbg(dev, fmt, ...)                        \dynamic_dev_dbg(dev, dev_fmt(fmt), ##__VA_ARGS__)
#elif defined(DEBUG)
#define dev_dbg(dev, fmt, ...)                        \dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__)
#else
#define dev_dbg(dev, fmt, ...)                        \
({                                    \if (0)                                \dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
})
#endif

三.相关配置

3.1 内核配置和DEBUG宏是如何影响dynamic debug API的输出的?

CONFIG_DYNAMIC_DEBUG

CONFIG_DEBUG_FS

DEBUG

输出情况

y

define

  1. 默认输出dynamic debug log。

  1. 可用dynamic debug命令控制。

y

no define

  1. 默认不输出dynamic debug log。

  1. 可用dynamic debug命令控制。

n

define

  1. 默认输出dynamic debug log。

  1. 不可用dynamic debug命令控制。

n

no define

  1. 默认不输出dynamic debug log。

  1. 不可用dynamic debug命令控制。

3.2 如何检查配置?

  • CONFIG_DYNAMIC_DEBUG, CONFIG_DEBUG_FS

在系统的编译配置文件 config中,如果为y,表示开启,为n,表示关闭。

  • DEBUG

  1. 方法1:在内核模块c源文件最开始处定义。

#define DEBUG//其他源码
xxxx
  1. 方法2:在内核模块的Makefile中定义。

ccflags-y += -DDEBUG

四. 如何控制dynamic debug logging?

4.1 在系统启动时

在系统启动参数中添加rule,rule为dynamic debug的控制规则。"QUERY"代表控制规则。

dyndbg="QUERY"
module.dyndbg="QUERY"

4.2 在内核模块加载时(foo为示例内核模块名)

当一个内核模块加载时, 会按照以下顺序检查以下三个设定。

When modprobe foo is called, modprobe scans /proc/cmdline for foo.params, strips foo., and passes them to the kernel along with params given in modprobe args or /etc/modprob.d/*.conf files, in the following order:

  1. parameters given via /etc/modprobe.d/*.conf:

options foo dyndbg=+pt
options foo dyndbg # defaults to +p
  1. foo.dyndbg as given in boot args, foo. is stripped and passed:

foo.dyndbg=" func bar +p; func buz +mp"
  1. args to modprobe:

modprobe foo dyndbg==pmf # override previous settings

4.2 在内核模块runing时

echo "module module_name +p" > /proc/dynamic_debug/control// enable the message at line 1603 of file svcsock.c
:#> ddcmd 'file svcsock.c line 1603 +p'// enable all the messages in file svcsock.c
:#> ddcmd 'file svcsock.c +p'// enable all the messages in the NFS server module
:#> ddcmd 'module nfsd +p'// enable all 12 messages in the function svc_process()
:#> ddcmd 'func svc_process +p'// disable all 12 messages in the function svc_process()
:#> ddcmd 'func svc_process -p'// enable messages for NFS calls READ, READLINK, READDIR and READDIR+.
:#> ddcmd 'format "nfsd: READ" +p'// enable messages in files of which the paths include string "usb"
:#> ddcmd 'file *usb* +p' > /proc/dynamic_debug/control// enable all messages
:#> ddcmd '+p' > /proc/dynamic_debug/control// add module, function to all enabled messages
:#> ddcmd '+mf' > /proc/dynamic_debug/control// boot-args example, with newlines and comments for readability
Kernel command line: ...// see what's going on in dyndbg=value processingdynamic_debug.verbose=3// enable pr_debugs in the btrfs module (can be builtin or loadable)btrfs.dyndbg="+p"// enable pr_debugs in all files under init/// and the function parse_one, #cmt is strippeddyndbg="file init/* +p #cmt ; func parse_one +p"// enable pr_debugs in 2 functions in a module loaded laterpc87360.dyndbg="func pc87360_init_device +p; func pc87360_find +p"

五. 示例

  • Makefile

#ccflags-y += -DDEBUGobj-m += test.oall:make -C /lib/modules/`uname -r`/build M=$(PWD) modules
clean:make -C /lib/modules/`uname -r`/build M=$(PWD) clean
  • source code

//#define DEBUG#include 
#include 
#include 
#include static int __init init_my_module(void) {pr_debug("i'm %s\n", "pr_debug");pr_info("i'm %s\n", "pr_info");pr_notice("i'm %s\n", "pr_notice");pr_warn("i'm %s\n", "pr_warn");pr_err("i'm %s\n", "pr_err");pr_crit("i'm %s\n", "pr_crit");pr_alert("i'm %s\n", "pr_alert");pr_emerg("i'm %s\n", "pr_emerg");dev_dbg(NULL, "i'm %s\n", "dev_dbg");dev_info(NULL, "i'm %s\n", "dev_info");dev_notice(NULL, "i'm %s\n", "dev_notice");dev_warn(NULL, "i'm %s\n", "dev_warn");dev_err(NULL, "i'm %s\n", "dev_err");dev_crit(NULL, "i'm %s\n", "dev_crit");dev_alert(NULL, "i'm %s\n", "dev_alert");dev_emerg(NULL, "i'm %s\n", "dev_emerg");return 0;
}static void __exit exit_my_module(void) {printk("Bye, my module!\n");
}module_init(init_my_module);
module_exit(exit_my_module);MODULE_LICENSE("GPL");
MODULE_AUTHOR("vec");

相关内容

热门资讯

122.(leaflet篇)l... 听老人家说:多看美女会长寿 地图之家总目录(订阅之前建议先查看该博客) 文章末尾处提供保证可运行...
育碧GDC2018程序化大世界... 1.传统手动绘制森林的问题 采用手动绘制的方法的话,每次迭代地形都要手动再绘制森林。这...
育碧GDC2018程序化大世界... 1.传统手动绘制森林的问题 采用手动绘制的方法的话,每次迭代地形都要手动再绘制森林。这...
Vue使用pdf-lib为文件... 之前也写过两篇预览pdf的,但是没有加水印,这是链接:Vu...
PyQt5数据库开发1 4.1... 文章目录 前言 步骤/方法 1 使用windows身份登录 2 启用混合登录模式 3 允许远程连接服...
Android studio ... 解决 Android studio 出现“The emulator process for AVD ...
Linux基础命令大全(上) ♥️作者:小刘在C站 ♥️个人主页:小刘主页 ♥️每天分享云计算网络运维...
再谈解决“因为文件包含病毒或潜... 前面出了一篇博文专门来解决“因为文件包含病毒或潜在的垃圾软件”的问题,其中第二种方法有...
南京邮电大学通达学院2023c... 题目展示 一.问题描述 实验题目1 定义一个学生类,其中包括如下内容: (1)私有数据成员 ①年龄 ...
PageObject 六大原则 PageObject六大原则: 1.封装服务的方法 2.不要暴露页面的细节 3.通过r...
【Linux网络编程】01:S... Socket多进程 OVERVIEWSocket多进程1.Server2.Client3.bug&...
数据结构刷题(二十五):122... 1.122. 买卖股票的最佳时机 II思路:贪心。把利润分解为每天为单位的维度,然后收...
浏览器事件循环 事件循环 浏览器的进程模型 何为进程? 程序运行需要有它自己专属的内存空间࿰...
8个免费图片/照片压缩工具帮您... 继续查看一些最好的图像压缩工具,以提升用户体验和存储空间以及网站使用支持。 无数图像压...
计算机二级Python备考(2... 目录  一、选择题 1.在Python语言中: 2.知识点 二、基本操作题 1. j...
端电压 相电压 线电压 记得刚接触矢量控制的时候,拿到板子,就赶紧去测各种波形,结...
如何使用Python检测和识别... 车牌检测与识别技术用途广泛,可以用于道路系统、无票停车场、车辆门禁等。这项技术结合了计...
带环链表详解 目录 一、什么是环形链表 二、判断是否为环形链表 2.1 具体题目 2.2 具体思路 2.3 思路的...
【C语言进阶:刨根究底字符串函... 本节重点内容: 深入理解strcpy函数的使用学会strcpy函数的模拟实现⚡strc...
Django web开发(一)... 文章目录前端开发1.快速开发网站2.标签2.1 编码2.2 title2.3 标题2.4 div和s...