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");

相关内容

热门资讯

安卓系统车机互联软件 你有没有发现,现在开车的时候,手机和车机之间的互动越来越频繁了呢?想象你正驾驶着爱车,一边享受着音乐...
荣耀play4安卓系统,智能生... 你有没有发现,最近手机圈里又掀起了一股热潮?没错,就是荣耀Play4这款新机!它不仅外观时尚,性能强...
安卓模拟苹果多开系统,打造跨平... 你有没有想过,在安卓手机上也能享受到苹果系统的多开功能呢?没错,这就是今天我要跟你分享的神奇话题——...
广汽埃安是安卓系统吗,搭载安卓... 你有没有想过,你的爱车广汽埃安,它的操作系统是不是安卓呢?这个问题,估计不少车友都在心里默默琢磨过。...
安卓系统是google的吗,谷... 你有没有想过,安卓系统是不是谷歌的亲生孩子呢?这个问题,就像是在问苹果是不是苹果树的宝宝一样,让人好...
安卓系统的设置程序下载,轻松掌... 你有没有发现,手机里的安卓系统设置程序真是五花八门,让人眼花缭乱?今天,就让我带你一起探索这个神秘的...
安卓系统的手机家园,打造个性化... 你有没有发现,现在手机的世界里,安卓系统的手机家园可是越来越热闹了呢!想象每天打开手机,就像走进了一...
安卓系统哪些相机可以调,支持实... 你有没有发现,手机拍照已经成为我们生活中不可或缺的一部分呢?无论是记录生活的点滴,还是捕捉美丽的瞬间...
安卓手机系统英文变中文,And... 你有没有发现,你的安卓手机上那些英文字母的系统提示,突然间变成了熟悉的中文呢?是不是觉得瞬间亲切感倍...
系统语言可以删除吗安卓,基于系... 你有没有想过,手机里的那些系统语言,是不是有时候觉得有点多余呢?比如,你是个地道的英语使用者,却总是...
安卓系统收取专利吗,揭秘科技巨... 你有没有想过,那个陪伴你每天刷手机、玩游戏、办公的安卓系统,它背后是不是也有自己的“小金库”呢?没错...
安卓屏幕怎么更新系统,轻松掌握... 亲爱的安卓手机用户们,你是不是也和我一样,时不时地想给手机来个“大变身”,让它焕然一新呢?没错,说的...
安卓系统有没有类似carpla... 你有没有想过,在安卓系统上,能不能也像苹果用户那样,轻松连接汽车音响系统呢?没错,我说的就是那个神奇...
安卓系统哪里升级包,生成与更新... 亲爱的安卓用户们,你是不是也和我一样,时不时地想给手机来个“大变身”,让它焕然一新呢?那就得提到那个...
安卓手机怎么恢复老系统,轻松恢... 手机用久了,是不是觉得系统越来越卡,功能也越来越不跟得上潮流了呢?别急,今天就来教你怎么把安卓手机恢...
安卓系统如何安装windows... 亲爱的安卓用户们,你是否曾幻想过在手机上体验Windows 7的韵味?别再羡慕那些拥有Windows...
旧的安卓系统怎么备份,轻松保存... 亲爱的安卓用户们,你是否曾经因为系统升级或者手机意外重启而担心丢失了珍贵的照片、联系人或者应用数据呢...
安卓手机系统文件被破坏,全面解... 手机突然间罢工了,是不是很崩溃?别急,今天就来聊聊安卓手机系统文件被破坏的那些事儿。相信我,掌握了这...
街头霸王四安卓系统,畅享格斗盛... 你知道吗?最近在安卓系统上,有一款游戏可是火得一塌糊涂,那就是《街头霸王四》!这款经典格斗游戏在安卓...
阿里tv安卓系统刷机,畅享智能... 你有没有发现,家里的阿里TV用久了,系统有点卡卡的呢?别急,今天就来教你怎么给它来个焕然一新的刷机大...